有人告诉我,在 WordPress 中注册和排队样式和脚本非常重要,尽管这在背后确实很痛苦。我对脚本没有问题,但是当我尝试样式时,WordPress 在样式表代码中没有显示任何内容。我想养成以正确方式构建主题的习惯,所以我想了解这是如何完成的,但我不明白为什么在添加脚本时我的 css 没有添加到代码中。请参阅下面的函数文件代码:
<?php
function alpha_scripts(){
$script = get_template_directory_uri() . '/assets/scripts/';
wp_register_script('bootstrap-js', $script . 'bootstrap.min.js', array('jquery'),'', false);
//wp_enqueue_script('jquery');
wp_enqueue_script('bootstrap-js');
}
function alpha_styles(){
$style = get_template_directory_uri() . '/assets/styles/';
wp_register_style('bootstrap-css', $style . 'bootstrap.min.css','','', 'screen');
wp_register_style('alpha-css', $style . 'alpha.css','','', 'screen');
wp_enqueue_style('bootstrap-css');
wp_enqueue_style('alpha-css');
}
function alpha_menus() {
register_nav_menus(
array(
'main-menu' => __( 'Primary Menu' ),
'footer-menu' => __( 'Footer Menu' )
)
);
}
add_action('wp_enqueue_scripts', 'alpha_scripts');
add_action('wp_enqueue_styles', 'alpha_styles');
add_action('after_setup_theme', 'alpha_menus');
?>
我在这里做错了什么?我试过对 url 使用不同的引用,比如 get_template_directory_uri()
和 get_stylesheet_directory_uri()
但我怀疑这两个没有区别。
最佳答案
看来你的注册和入队是正确的;但我怀疑函数本身没有被调用,因为您附加到它的操作不是 WordPress 定义的。
add_action('wp_enqueue_styles', 'alpha_styles');
应该是:
add_action('wp_enqueue_scripts', 'alpha_styles');
wp_enqueue_scripts 是应该用于脚本 和 样式的钩子(Hook)。 (参见:https://codex.wordpress.org/Plugin_API/Action_Reference/wp_enqueue_scripts)
如果您对将来的 Hook 不确定,可以在此处的操作引用中检查它是否存在:https://codex.wordpress.org/Plugin_API/Action_Reference
关于css - 尝试在 WP 中正确注册和排队样式表,但没有呈现任何内容,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/37038215/