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

WordPress 有些时候为个性化主题,我们会制作子主题方便修改主题功能外观,而不用修改原主题,通常我们会用: @import url( '../parent-theme-folder/style.css' ); 这种方式加载父主题的样式。 但使用@ import方式加载样式也有一定的缺点,比如:加载慢等。 我们可以...
Wordpress另一种加载父主题样式的方法
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' );
}

给TA打赏
共{{data.count}}人
人已打赏
wordpress教程

WordPress点击文章显示加载中…特效

2023-2-16 23:45:59

wordpress教程

wordpress固定链接设置的一些心得

2023-2-16 23:46:02

【腾讯云】11.11云上盛惠!云服务器首年1.8折起,买1年送3个月!
11.11云上盛惠!海量产品·轻松上云!云服务器首年1.8折起,买1年送3个月!超值优惠,性能稳定,让您的云端之旅更加畅享。
查看更多相关信息>>
站长

(工作日 10:00 - 22:30 为您服务)

2026-03-27 19:34:11

您好,无论是售前、售后、意见建议……均可通过联系工单与我们取得联系。

猜你想问:

  • 购买的模板免费包安装吗?

  • 这个演示地址有吗?

  • 购买vip会员可以下载哪些模板?

您的留言我们已经收到,我们将会尽快跟您联系!
取消
立即选择任一渠道联系我们