最新活动:买一送一!升级会员,最高返 500 抵扣券!>>>

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

!
你也想出现在这里?立即 联系我们吧!
信息
0 条回复 A文章作者 M管理员
    暂无讨论,说说你的看法吧
个人中心
购物车
优惠劵
今日签到
搜索