wordpress child 主题加载 child 样式两次,没有 parent 样式

wordpress child theme loads child style twice, no parent style

WP 法典的所有 self-contradictions 和关于创建 child 主题的错误信息让我纠结不已。 child 主题页面说我只需要 style.css 和 functions.php。那是错误的。 parent 主题是 twentyfifteen。我尝试了推荐的模式,然后按照结果 PHP 错误添加了投诉中提到的所有文件。我的 child 主题 twentyfifteen-child 现在包含:footer.php、functions.php、header.php、index.php、sidebar.php 和 style.css.

child style.css 文件仅包含样式sheet header:

Theme Name: Twenty Fifteen Child
Theme URI: http://dwp.avionicsspecialists.net/wordpress/wp-content/themes/twenty-fifteen-child/

因为法典是矛盾的,所以我尝试了主题 URI 文件夹的变体:twenty-fifteen-child、twentyfifteen-child 和 twentyfifteen。 None 的变化影响了结果。

functions.php 文件包含以下排队代码。

add_action( 'wp_enqueue_scripts', 'theme_enqueue_styles' );
function theme_enqueue_styles() {
    $parent_style = 'parent-style';

    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 )
    );
}

我直接从parent twentyfifteen 主题复制的其他文件没有任何改动。

当我激活 child 主题时,它会显示一个完全没有样式的 WP 站点。它加载 child 样式 sheet 两次,而不是 parent 样式 sheet。 Codex 说 get_template_directory_uri() 总是 returns parent 主题路径。不对。在我的主题中,它 returns child 主题路径。在数据库中,wp_options.option_name = 'template' 的值为 'twentyfifteen-child'.

如果我将该值手动更改为 'twentyfifteen',我会得到更接近我需要的结果。这种情况会同时加载 parent 和 child 样式 sheet。但是,手动更改数据库不是有效的解决方案。

我对变体没有想法。非常感谢任何帮助。

确保 style.css 主题的 header 是 CSS 评论(它应该以 /* 开头并以 */ 结尾)并且它有这一行:

/* 
Template:     twentyfifteen
*/

编程经验法则:

Don't delete code you do not fully understand. Sooner or later it will hit the fan.

有关 child 主题及其 header 的更多详细信息 here

The comment header lines in style.css are required for WordPress to be able to identify the Theme and display it in the Administration Panel under Design > Themes as an available Theme option along with any other installed Themes.

在 wordpress 中创建子主题所需的唯一项目是 style.css 中的此信息:

   /*
   * Theme Name: Your Child Theme Name
   * Author: Who you are
   * Version: 1.0
   * Template: twentyfifteen
   */

您不需要在 functions.php 中包含 URI 函数。