首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >用PHP在子主题中包括style.css

用PHP在子主题中包括style.css
EN

WordPress Development用户
提问于 2018-11-02 13:41:46
回答 1查看 944关注 0票数 0

https://codex.wordpress.org/Child_主题中,将style.css包含在子主题中的最佳实践必须将下面的代码放入您的子主题的functions.php中,但将“父样式”替换为父主题的参数,这可以在父主题的functions.php中找到。我在我的主题文件中找不到这一点,而且没有太多的代码。使用@import包含父主题样式表的方法不适用于我,放在文件中的css没有在任何浏览器中在站点上显示自己。

代码语言:javascript
复制
function my_theme_enqueue_styles() {

    $parent_style = 'parent-style'; // This is 'twentyfifteen-style' for the Twenty Fifteen theme.

    wp_enqueue_style( $parent_style, get_template_directory_uri() . '/style.css' );
    wp_enqueue_style( 'child-style',
        get_stylesheet_directory_uri() . '/style.css',
        array( $parent_style ),
        wp_get_theme()->get('Version')
    );
}
add_action( 'wp_enqueue_scripts', 'my_theme_enqueue_styles' );

我有一个关于这个队列的工作示例,但是对于一个不同的主题:

代码语言:javascript
复制
/**
 * Enqueue the parent theme stylesheet.
 */

add_action( 'wp_enqueue_scripts', 'vantage_parent_style' );
function vantage_parent_style() {
    wp_enqueue_style( 'parent-theme', get_template_directory_uri() . '/style.css' );
}

/**
 * Enqueue the child theme stylesheet.
 */

add_action( 'wp_enqueue_scripts', 'vantage_child_style', 20 );
function vantage_child_style() {
    wp_enqueue_style( 'child-theme', get_stylesheet_uri() );
}

我的主题名为Samsklad,我不知道如何在我的functions.php子主题中实现代码,我对PHP不太在行。

EN

回答 1

WordPress Development用户

回答已采纳

发布于 2018-11-02 15:27:13

您在评论中提到当前主题在其header.php中使用了以下代码

代码语言:javascript
复制

这不是WordPress最佳实践,因此链接教程会让您失望。

如何从这继续下去?

将header.php从父主题复制到子主题,删除包含style.css的行,然后可以将以下代码放在子主题的functions.php中:

代码语言:javascript
复制
function my_theme_enqueue_styles() {
    wp_register_style( 'parent-style', get_template_directory_uri() . '/style.css' );
    wp_enqueue_style( 'child-style',
        get_stylesheet_directory_uri() . '/style.css',
        array( 'parent-style' ),
        wp_get_theme()->get('Version')
    );
}
add_action( 'wp_enqueue_scripts', 'my_theme_enqueue_styles' );
票数 1
EN
页面原文内容由WordPress Development提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://wordpress.stackexchange.com/questions/318269

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档