另一种加载父主题样式的方法

有些时候为个性化主题,我们会制作子主题方便修改主题功能外观,而不用修改原主题,通常我们会用:

  1.  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方式加载样式也有一定的缺点,比如:加载慢等。

我们可以使用另一种加载父主题样式的方法:

  1. add_action( \’wp_enqueue_scripts\’, \’my_parent_theme_css\’ );
  2.  my_parent_theme_css() {
  3.     wp_enqueue_style( \’parent-style\’, get_template_directory_uri() . \’/style.css\’ );
  4.     wp_enqueue_style( \’child-style\’, get_stylesheet_uri(), ( \’parent-style\’ ) );
  5. }

将代码添加到子主题functions.php 模板文件中。

还可以进一步:

  1. add_action( \’wp_enqueue_scripts\’, \’my_enqueue_styles\’ );
  2.  my_enqueue_styles() {
  3.     
  4.      ( is_child_theme() ) {
  5.         wp_enqueue_style( \’parent-style\’, trailingslashit( get_template_directory_uri() ) . \’style.css\’ );
  6.     }
  7.     
  8.     wp_enqueue_style( \’style\’, get_stylesheet_uri() );
  9. }

在某些特殊的情况下,你可能不希望加载父主题的样式,可以这样写:

  1. add_action( \’wp_enqueue_scripts\’, \’my_dequeue_styles\’, 11 );
  2.  my_dequeue_styles() {
  3.     wp_dequeue_style( \’parent-style\’ );
  4. }

原文:http://justintadlock.com/archives/2014/11/03/loading-parent-styles-for-child-themes

    © 版权声明
    THE END
    喜欢就支持一下吧
    点赞0 分享
    评论 抢沙发

    请登录后发表评论

      暂无评论内容