我是WordPress开发的新手。我读到在functions.php文件中使用wp_enqueue_style和do_action要比我不使用WordPress时直接链接CSS文件要好。
为什么这是最佳实践?它的优势是什么?
发布于 2017-01-24 18:32:11
如果你已经激活了子主题,那么就使用get_template_directory_uri()函数。
如果你已经激活了父主题,那么就使用get_stylesheet_directory_uri()函数。
将始终引用资源的父主题文件夹。
将引用资源的“当前”主题文件夹(可能是父文件夹或子文件夹,这取决于它被调用的位置)。
子主题示例:
wp_enqueue_style( 'my_child_styles', get_stylesheet_directory_uri().'/style.css' );父主题示例
wp_enqueue_style( 'my_parent_styles', get_template_directory_uri().'/style.css' );方法-1
// load css into the website's front-end
function mytheme_enqueue_style() {
wp_enqueue_style( 'mytheme-style', get_stylesheet_directory_uri().'/style.css' );
}
add_action( 'wp_enqueue_scripts', 'mytheme_enqueue_style' );方法-2
// Add this code in your functions.php
function add_stylesheet_to_head() {
echo "<link href='".get_stylesheet_directory_uri()."/style.css' rel='stylesheet' type='text/css'>";
}
add_action( 'wp_head', 'add_stylesheet_to_head' );发布于 2017-01-24 19:04:18
因为wp-enqueue-style将脚本/样式添加到队列中。
如果提供了源(不覆盖)并入队,则注册样式。
https://stackoverflow.com/questions/41825241
复制相似问题