带有依赖项的子主题排队

din*_*o_d 5 php wordpress

我正在尝试创建一个子主题。父主题有一个 style.css 和所有,我在看wp_enqueue_style()函数,它说你可以引入依赖项。所以这意味着主题自己style.css可以是活动的,并且在我的子主题中,如果我在 style.css 中指定相同的规则,它应该覆盖它。

但是依赖是一个句柄数组。我如何找到这些句柄?

wp_enqueue_style( 'mytheme-style', get_stylesheet_directory_uri().'/style.css', array('main_css') );
Run Code Online (Sandbox Code Playgroud)

我尝试了上面的方法,但它只从子主题加载 style.css,而不是从父主题加载。

我在哪里可以找到这些手柄?

编辑:

我找到了重现句柄和脚本的代码:

function wpa54064_inspect_scripts() {
    global $wp_scripts;
    foreach( $wp_scripts->queue as $handle ) :
        echo $handle,' ';
    endforeach;
}
add_action( 'wp_print_scripts', 'wpa54064_inspect_scripts' );

function wpa54064_inspect_style() {
    global $wp_styles;
    foreach( $wp_styles->queue as $handle ) :
        echo $handle,' ';
    endforeach;
}
add_action( 'wp_print_scripts', 'wpa54064_inspect_style' );
Run Code Online (Sandbox Code Playgroud)

但它仍然无法像我想象的那样工作。

Nat*_*son 2

get_stylesheet_directory_uri()如果子主题处于活动状态,将返回子主题 URL。

由于您尝试在父主题中加载 style.css 文件,get_template_directory_uri()因此您可以使用它。

例如:

wp_enqueue_style( 'mytheme-style', get_template_directory_uri() . '/style.css', array('main_css') );
Run Code Online (Sandbox Code Playgroud)

我的建议是像这样加载样式表(代码位于子主题的functions.php中):

function wpse_load_styles() {
    wp_enqueue_style( 'parent-styles', get_template_directory_uri() . '/style.css' );
    wp_enqueue_style( 'mytheme', get_stylesheet_uri(), array( 'parent-styles' ) );
}
add_action( 'wp_enqueue_scripts', 'wpse_load_styles' );
Run Code Online (Sandbox Code Playgroud)