【问题标题】:Why does wp_enqueue_style doesn't work here?为什么 wp_enqueue_style 在这里不起作用?
【发布时间】:2019-02-10 15:36:17
【问题描述】:

我正在开发一个关于 WordPress 的主题。我的 index.php 和 style.css 在同一个文件夹中,在主题目录的根目录下。我的 css 文件在“css”文件夹中,我的 js 文件在“js”文件夹中。我想将 CSS 和 JS 添加到 functions.php 文件中的索引(使用 wp enqueue 样式/脚本)

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');
}

但这不起作用。 我尝试了“if”条件,只尝试了 wp_enqueue_style 函数(以及我在 google 上找到的许多其他解决方案),但这些都不起作用。我不知道问题是什么。

这是file tree的截图

编辑:它不起作用,因为在 css 中没有链接到 php 文件,它显示纯 HTML 文本。如果我打开开发工具,它不会显示任何 CSS 属性。

Like that

谢谢!

【问题讨论】:

  • 检查wp_register_style返回什么?
  • 我不确定你的意思,wp_register_style 怎么能真正返回一些东西?
  • “这些都不起作用”究竟是什么意思?您的标记中是否输出了错误的路径?是否有任何错误消息,无论是 WP 本身还是您的浏览器控制台抛出的?您可以将其添加到您的问题中吗?
  • 我编辑了帖子,基本上它只是显示纯文本(添加了显示内容的屏幕截图)
  • @MaelLandrin,wp_register_style 返回 true 或 false。

标签: javascript css wordpress styles


【解决方案1】:

wp_enqueue_* 和各自的 wp_register_* 函数之间的主要区别在于,第一个将脚本/样式添加到队列中,第二个准备要添加的脚本/样式。

您需要将添加操作移到函数之后,它会起作用:

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' );

以下更新答案:

为什么不直接将样式排入队列,而不是先注册然后将它们排入队列,如下所示:

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');');

}

add_action('wp_enqueue_scripts', 'theme_scripts');

【讨论】:

    猜你喜欢
    • 2013-05-16
    • 2017-10-11
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多