注册样式表时查找父主题的样式表$Handle

时间:2017-02-20 作者:The Chewy

我有一个子主题,我在CSS中使用旧的@import进行导入,我知道这不再是最佳实践。我在Wordpress codex上看到了如何正确地做到这一点,这似乎很简单,但我需要在父主题注册样式表时找到它所使用的$句柄。

这是他们给出的代码:

<?php
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\' );
?>
如何找出父主题的样式。css$句柄,以便我可以将其替换为上面使用的“父样式”?(我使用的主题是Divi,但我想知道如何找到它,因为我确实使用了其他主题)。

谢谢

艾米丽

3 个回复
SO网友:Boris Chevreau

为了澄清这一点,对于任何使用Divi主题(但通常是任何WP主题)的人来说,在样式表链接的“头部”使用Dev工具进行简单的检查通常会提供关于句柄的线索,正如Den Isahac之前提到的(由于他提到找不到,所以不清楚)。

Anything before the "-css" on the ID of the link for the stylesheet of the Parent theme is generally that handle.

下面的示例使用Divi,id=“Divi style css”因此$handle=\'Divi style\'

Above is template style with id='divi-style-css' , and below the child

因此,对于Divi,您可以像这样将主题排队:

<?php
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\' );
?>
没什么好处:浏览器有一种恼人的倾向,即根据其“版本”缓存子样式表。缺点是,即使在刷新/清除缓存后,也无法看到编辑,除非每次编辑后更改css文件中的“版本”号。

可以通过交换来更改此行为wp_get_theme()->get(\'Version\') ($ver参数)用于handyfilemtime( get_stylesheet_directory() . \'/style.css\' ) 相反,它在样式表之后添加一个版本号,该版本号与上次保存子样式表时的时间戳相对应,而不是在该样式表中声明的真正“版本”。您最终可以在上线之前恢复到常规功能,但这在生产过程中非常有用。

因此,Divi的整个排队脚本将变成:

<?php
function my_theme_enqueue_styles() {

    $parent_style = \'divi-style\'; // This is \'Divi\' style referrence for the Divi 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 ),
        filemtime( get_stylesheet_directory() . \'/style.css\' )
    );
}
add_action( \'wp_enqueue_scripts\', \'my_theme_enqueue_styles\' );
?>

SO网友:David Lee

这不可能使用主题,您需要手动执行,否则您必须找到plugin 这样可以保存该数据并在主题中检索该数据。

的优点plugin 如果父主题具有复杂的结构,则不必在每个文件中搜索wp_enqueue_style 和复制粘贴处理程序,您将使用get_option 或afunction 插件将提供您必须进入每个child-theme 并更新该值。

SO网友:Den Isahac

这个$handle 在本例中,它只是一个名称,用于标识所引用的样式表,在PHP中作为值进行硬编码,在任何地方都找不到。

事实上parent-style 将成为id ,WordPress将为您生成以下HTML代码:

<link id="parent-style-css" rel="stylesheet" href="THE PARENT STYLE CSS URL"/>

注意到parent-style ID中使用的值,然后后缀为css

相关推荐

Child-theme breaks site

所以,我有一个子主题,里面除了所需的CSS文件之外什么都没有。一旦我激活了这个儿童主题,我的整个网站就关闭了。最后我有两个问题:激活一个只有CSS的子主题怎么能破坏我的网站</我怎样才能回到我原来的主题</这些是网站给我的错误:Warning: require_once(/wp-content/themes/interio_child/admin/options-framework.php) [function.require-once]: 无法打开流:中没有此类文件或目录/wp-c