另一种加载父主题样式的方法
另一种加载父主题样式的方法
有些时候为个性化主题,我们会制作子主题方便修改主题功能外观,而不用修改原主题,通常我们会用:
- @import url( ‘../parent-theme-folder/style.css’ );
这种方式加载父主题的样式。
详见:
- Codex: How to create a child theme
- Handbook: Inheriting styles in child themes
- _s Theme: Load parent styles when child theme is activated
但使用@ import方式加载样式也有一定的缺点,比如:加载慢等。
我们可以使用另一种加载父主题样式的方法:
- add_action( ‘wp_enqueue_scripts’, ‘my_parent_theme_css’ );
- function my_parent_theme_css() {
- wp_enqueue_style( ‘parent-style’, get_template_directory_uri() . ‘/style.css’ );
- wp_enqueue_style( ‘child-style’, get_stylesheet_uri(), array( ‘parent-style’ ) );
- }
将代码添加到子主题functions.php 模板文件中。
还可以进一步:
- add_action( ‘wp_enqueue_scripts’, ‘my_enqueue_styles’ );
- function my_enqueue_styles() {
- /* If using a child theme, auto-load the parent theme style. */
- if ( is_child_theme() ) {
- wp_enqueue_style( ‘parent-style’, trailingslashit( get_template_directory_uri() ) . ‘style.css’ );
- }
- /* Always load active theme’s style.css. */
- wp_enqueue_style( ‘style’, get_stylesheet_uri() );
- }
在某些特殊的情况下,你可能不希望加载父主题的样式,可以这样写:
- add_action( ‘wp_enqueue_scripts’, ‘my_dequeue_styles’, 11 );
- function my_dequeue_styles() {
- wp_dequeue_style( ‘parent-style’ );
- }
原文:http://justintadlock.com/archives/2014/11/03/loading-parent-styles-for-child-themes

下载说明:
1. 本站所有资源来源于网络和用户上传,如有侵权请联系站长
2. 本站分享目的仅供大家学习和交流,您必须在下载后24小时内删除!
3. 不得使用于非法商业用途,商用请支持正版!不得违反国家法律,否则后果自负!
6. 本站资源售价只是赞助,收取费用仅维持本站的日常运营所需!
本站的资源均来自于互联网,仅为资源共享、学习参考之目的,其版权均归原作者及其网站所有,如有侵权请留言联系:admin,转转请注明出处:http://zhanmr.com/3729.htm