简体   繁体   English

在WordPress中正确创建子主题的困惑

[英]Confusion of creating a child theme properly in wordpress

Trying to create a child theme from wordpress twentyfifteen theme. 尝试从wordpress二十十五主题创建一个子主题。 Wordpress codex says WordPress的法典说

Note that the previous method was to import the parent theme stylesheet using @import: this is no longer best practice. 请注意,以前的方法是使用@import导入父主题样式表:这不再是最佳实践。 The correct method of enqueuing the parent theme stylesheet is to use wp_enqueue_script() in your child theme's functions.php. 使父主题样式表排队的正确方法是在子主题的functions.php中使用wp_enqueue_script()。

The function which is responsible of loading styles and javascript files of twentyfifteen is 负责加载25个样式和javascript文件的函数是

function twentyfifteen_scripts() {
    // Add custom fonts, used in the main stylesheet.
    wp_enqueue_style( 'twentyfifteen-fonts', twentyfifteen_fonts_url(), array(), null );

    // Add Genericons, used in the main stylesheet.
    wp_enqueue_style( 'genericons', get_template_directory_uri() . '/genericons/genericons.css', array(), '3.2' );

    // Load our main stylesheet.
    wp_enqueue_style( 'twentyfifteen-style', get_stylesheet_uri() );

    // Load the Internet Explorer specific stylesheet.
    wp_enqueue_style( 'twentyfifteen-ie', get_template_directory_uri() . '/css/ie.css', array( 'twentyfifteen-style' ), '20141010' );
    wp_style_add_data( 'twentyfifteen-ie', 'conditional', 'lt IE 9' );

    // Load the Internet Explorer 7 specific stylesheet.
    wp_enqueue_style( 'twentyfifteen-ie7', get_template_directory_uri() . '/css/ie7.css', array( 'twentyfifteen-style' ), '20141010' );
    wp_style_add_data( 'twentyfifteen-ie7', 'conditional', 'lt IE 8' );

    wp_enqueue_script( 'twentyfifteen-skip-link-focus-fix', get_template_directory_uri() . '/js/skip-link-focus-fix.js', array(), '20141010', true );

    if ( is_singular() && comments_open() && get_option( 'thread_comments' ) ) {
        wp_enqueue_script( 'comment-reply' );
    }

    if ( is_singular() && wp_attachment_is_image() ) {
        wp_enqueue_script( 'twentyfifteen-keyboard-image-navigation', get_template_directory_uri() . '/js/keyboard-image-navigation.js', array( 'jquery' ), '20141010' );
    }

    wp_enqueue_script( 'twentyfifteen-script', get_template_directory_uri() . '/js/functions.js', array( 'jquery' ), '20141212', true );
    wp_localize_script( 'twentyfifteen-script', 'screenReaderText', array(
        'expand'   => '<span class="screen-reader-text">' . __( 'expand child menu', 'twentyfifteen' ) . '</span>',
        'collapse' => '<span class="screen-reader-text">' . __( 'collapse child menu', 'twentyfifteen' ) . '</span>',
    ) );
}
add_action( 'wp_enqueue_scripts', 'twentyfifteen_scripts' );

So after copy it from parent's functions.php and paste it in child's functions.php what i did : 1.Changed the function name. 因此,从父代的functions.php复制它并将其粘贴到子代的functions.php ,我做了什么:1.更改了函数名称。 2.Replaced the line 2.更换线

wp_enqueue_style( 'twentyfifteen-style', get_stylesheet_uri() );

with

wp_enqueue_style( 'parent-style', get_template_directory_uri() . '/style.css' );

3.Removed code for javascript files. 3.删除了javascript文件的代码。

Do i also remove other style sheets which are not main style sheet of parent theme? 我还会删除不是父主题的主要样式表的其他样式表吗? How do i include another stylesheet properly which are in the child's theme ? 如何正确包含儿童主题中的另一个样式表? (do i just use wp_enqueue_style( 'child-style', get_stylesheet_directory_uri() . '/style.css' ); ?) (我只使用wp_enqueue_style( 'child-style', get_stylesheet_directory_uri() . '/style.css' );吗?

You don't need much to make a child theme, most of the code in your question is not needed: 您不需要太多的时间来制作子主题,不需要使用问题中的大多数代码:

  1. create a folder in your 'themes' directory and name it whatever you'd like. 在“主题”目录中创建一个文件夹,并根据需要命名。 twentyfifteenchild will make it clear 二十五岁的孩子会说清楚
  2. create a file called style.css and put the following at the top: 创建一个名为style.css的文件,并将以下内容放在顶部:

     /* Theme Name: Twenty Fifteen Child Theme URI: http://example.com/twenty-fifteen-child/ Description: Twenty Fifteen Child Theme Author: John Doe Author URI: http://example.com Template: twentyfifteen Version: 1.0.0 License: GNU General Public License v2 or later License URI: http://www.gnu.org/licenses/gpl-2.0.html Tags: light, dark, two-columns, right-sidebar, responsive-layout, accessibility-ready Text Domain: twenty-fifteen-child */ 
  3. Create a functions.php file and paste the following into it: 创建一个functions.php文件,并将以下内容粘贴到其中:

     <?php add_action( 'wp_enqueue_scripts', 'theme_enqueue_styles' ); function theme_enqueue_styles() { wp_enqueue_style( 'parent-style', get_template_directory_uri() . '/style.css' ); } 

Weird, I can answer but not comment yet... I also want to know why people use the @import in the CSS, when WordPress specifically says this is the way: 很奇怪,我可以回答但还不能发表评论...我也想知道为什么人们在CSS中使用@import的原因,而WordPress特别指出了这种方式:

The following example function will only work if your Parent Theme uses only one main style.css to hold all of the css. 以下示例函数仅在您的父主题仅使用一种主要style.css来保存所有CSS时才起作用。 If your theme has more than one .css file (eg. ie.css, style.css, main.css) then you will have to make sure to maintain all of the Parent Theme dependencies. 如果您的主题有多个.css文件(例如,.css,style.css,main.css),则必须确保维护所有父主题依赖项。

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

}

Setting 'parent-style' as a dependency will ensure that the child theme stylesheet loads after it. 将“父样式”设置为依赖项将确保子主题样式表在其后加载。 See here a more detailed discussion : 看到这里更详细的讨论:

add_action( 'wp_enqueue_scripts', 'theme_enqueue_styles' );
function theme_enqueue_styles() {
    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')
    );
}

I actually learned now that I didn't always do the dependencies thingy... noob/designer fail or bad documentation fail... ?!? 现在我实际上了解到,我并不总是那样做依赖项……noob / designer失败或不良的文档失败...?!?

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM