简体   繁体   English

为什么wp_enqueue_style在这里不起作用?

[英]Why does wp_enqueue_style doesn't work here?

I'm developing a theme on WordPress. 我正在用WordPress开发主题。 My index.php and style.css are in the same folder, at the root of the theme directory. 我的index.php和style.css位于主题目录根目录下的同一文件夹中。 My css files are in a "css" folder, my js files are in "js" folder. 我的css文件位于“ css”文件夹中,我的js文件位于“ js”文件夹中。 I want to add the CSS and JS to the index ( with the wp enqueue style/script ) in the functions.php file 我想将CSS和JS添加到functions.php文件中的索引(带有wp排队样式/脚本)中

add_action( 'wp_enqueue_scripts', 'add_stylesheet' );
function add_stylesheet() {
    wp_register_style('style', get_template_directory_uri() . '/style.css');
    wp_register_style('plugins', get_template_directory_uri() . '/css/plugins.css');
    wp_register_style('nav', get_template_directory_uri() . '/css/navigation-menu.css');
    wp_register_style('shortcode', get_template_directory_uri() . '/css/shortcodes.css');
    wp_enqueue_style('style');
    wp_enqueue_style('plugins');
    wp_enqueue_style('nav');
    wp_enqueue_style('shortcode');
}

But that doesn't work. 但这是行不通的。 I tried with a 'if' condition, I tried with just the wp_enqueue_style function ( and many other solutions I found on google ), but none of these work. 我尝试了'if'条件,仅尝试了wp_enqueue_style函数(以及我在google上找到的许多其他解决方案),但是这些都不起作用。 I have no idea what the problem is. 我不知道是什么问题。

Here's a screenshot of the file tree 这是文件树的屏幕截图

Edit : It doesn't work as in the css is not linked to the php files, it shows plain HTML text. 编辑:它不起作用,因为在CSS中未链接到php文件,它显示纯HTML文本。 If I open the dev tools, it shows no CSS properties. 如果打开开发工具,则不会显示CSS属性。

Like that 像那样

Thanks ! 谢谢 !

The main difference between wp_enqueue_* and respective wp_register_* functions, is that the first adds scripts/styles to the queue and the second prepares scripts/styles to be added. wp_enqueue_ *和各自的wp_register_ *函数之间的主要区别在于,第一个将脚本/样式添加到队列中,第二个准备要添加的脚本/样式。

You need to move the add action after the function and it'll work: 您需要在该函数之后移动add动作,然后它将起作用:

function add_stylesheet() {
    wp_register_style('style', get_template_directory_uri() . '/style.css');
    wp_register_style('plugins', get_template_directory_uri() . '/css/plugins.css');
    wp_register_style('nav', get_template_directory_uri() . '/css/navigation-menu.css');
    wp_register_style('shortcode', get_template_directory_uri() . '/css/shortcodes.css');
    wp_enqueue_style('style');
    wp_enqueue_style('plugins');
    wp_enqueue_style('nav');
    wp_enqueue_style('shortcode');
}
add_action( 'wp_enqueue_scripts', 'add_stylesheet' );

Updated answer below: 更新的答案如下:

Why not just enqueue the styles instead of registering and then enqueuing them, like this: 为什么不将样式排入队列,而不是先注册然后将其排入队列,如下所示:

function theme_scripts()
{
    wp_enqueue_style('style', get_template_directory_uri() . '/style.css');

wp_enqueue_style('style', get_template_directory_uri() . 'wp_enqueue_style('plugins', get_template_directory_uri() . '/style.css');'); wp_enqueue_style('style',get_template_directory_uri()。'wp_enqueue_style('plugins',get_template_directory_uri()。'/style.css');');

}

add_action('wp_enqueue_scripts', 'theme_scripts');

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM