admin管理员组

文章数量:1384613

When creating child them and setting up the initial function.php content:

<?php
add_action( 'wp_enqueue_scripts', 'my_theme_enqueue_styles' );
function my_theme_enqueue_styles() {

$parent_style = 'parent-style'; // <=====HERE

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

In this exemple should I rename 'parent-style' as the name of the parent style? Or leave it as 'parent-style'?

When creating child them and setting up the initial function.php content:

<?php
add_action( 'wp_enqueue_scripts', 'my_theme_enqueue_styles' );
function my_theme_enqueue_styles() {

$parent_style = 'parent-style'; // <=====HERE

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

In this exemple should I rename 'parent-style' as the name of the parent style? Or leave it as 'parent-style'?

Share Improve this question asked Apr 23, 2020 at 16:13 Robert BraxRobert Brax 1353 bronze badges 1
  • 2 It’s not terribly important, but using a unique name, such as the theme name, reduces the chance of conflicts, although it’s unlikely a plugin will use the name ‘parent-style’. – Jacob Peattie Commented Apr 23, 2020 at 16:38
Add a comment  | 

1 Answer 1

Reset to default 1

You can leave it as is (see Jacob Pettie's comment) or you can change it, but the key here is to add the path to locate the parent theme’s CSS.

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

In your example you’re cutting it off after the ( ‘parent-style’ ), but you should continue and include the path to the parent theme’s style.css.

get_template_directory_uri() is for the parent theme.

The full step by step breakdown can be found here: https://developer.wordpress/themes/advanced-topics/child-themes/

本文标签: Child them functionphp 39parentstyle39 should I name this as parent theme name