【发布时间】: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 属性。
谢谢!
【问题讨论】:
-
检查
wp_register_style返回什么? -
我不确定你的意思,wp_register_style 怎么能真正返回一些东西?
-
“这些都不起作用”究竟是什么意思?您的标记中是否输出了错误的路径?是否有任何错误消息,无论是 WP 本身还是您的浏览器控制台抛出的?您可以将其添加到您的问题中吗?
-
我编辑了帖子,基本上它只是显示纯文本(添加了显示内容的屏幕截图)
-
@MaelLandrin,wp_register_style 返回 true 或 false。
标签: javascript css wordpress styles