首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >为什么在WordPress中使用wp_enqueue_style比使用链接标签更好?

问为什么在WordPress中使用wp_enqueue_style比使用链接标签更好?
EN

Stack Overflow用户
提问于 2017-01-24 18:03:59
回答 2查看 421关注 0票数 3

我是WordPress开发的新手。我读到在functions.php文件中使用wp_enqueue_style和do_action要比我不使用WordPress时直接链接CSS文件要好。

为什么这是最佳实践?它的优势是什么?

EN

回答 2

Stack Overflow用户

发布于 2017-01-24 18:32:11

如果你已经激活了子主题,那么就使用get_template_directory_uri()函数。

如果你已经激活了父主题,那么就使用get_stylesheet_directory_uri()函数。

将始终引用资源的父主题文件夹。

将引用资源的“当前”主题文件夹(可能是父文件夹或子文件夹,这取决于它被调用的位置)。

子主题示例:

代码语言:javascript
复制
wp_enqueue_style( 'my_child_styles', get_stylesheet_directory_uri().'/style.css' );

父主题示例

代码语言:javascript
复制
wp_enqueue_style( 'my_parent_styles', get_template_directory_uri().'/style.css' );

方法-1

代码语言:javascript
复制
// 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

代码语言:javascript
复制
// 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' );
票数 1
EN

Stack Overflow用户

发布于 2017-01-24 19:04:18

因为wp-enqueue-style将脚本/样式添加到队列中。

如果提供了源(不覆盖)并入队,则注册样式。

票数 1
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/41825241

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档