欢迎您光临自学哈网,只为分享网络知识教程,供大家学习参考!

「自学哈网」WordPress另一种加载父主题样式的方法

作者 : 自学哈 本文共909个字,预计阅读时间需要3分钟 2022-11-6 共111人阅读
也想出现在这里? 联系我们

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

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

@import url( '../parent-theme-folder/style.css' );

这种方式加载父主题的样式。

但使用@ 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' );}
本站声明:
本站所有资源来源于网络,分享目的仅供大家学习和交流!如若本站内容侵犯了原著者的合法权益,可联系邮箱976157886@qq.com进行删除。
自学哈专注于免费提供最新的分享知识、网络教程、网络技术的资源分享平台,好资源不私藏,大家一起分享!

自学哈网 » 「自学哈网」WordPress另一种加载父主题样式的方法
也想出现在这里? 联系我们
© 2022 Theme by - 自学哈网 & WordPress Theme. All rights reserved 浙ICP备2022016594号