wp_enqueue_style()会破坏子主题中的内容样式

时间:2016-08-31 13:58:57

标签: wordpress

我在父主题中包含bootstrap.css和style.css。

function my_scripts() { 
    wp_register_style('bootstrap', get_template_directory_uri() . '/css/bootstrap.css');    
    wp_register_style('my-style', get_stylesheet_uri());        
    wp_enqueue_style('bootstrap');      
    wp_enqueue_style('my-style');
    ....
}

在儿童主题中,我包括父母风格,之后设计崩溃了。当我在Firefox中看到源代码时,我看到父style.css首先加载然后加载。

function child_scripts() {  
        wp_enqueue_style( 'my-style', get_template_directory_uri(). '/style.css' );     
}

add_action( 'wp_enqueue_scripts', 'child_scripts', 15 );

如何解决此问题?

2 个答案:

答案 0 :(得分:2)

You can't use the same handle for multiple style files or they will overwrite one another. You are using "my-style" as a handle in both the parent and child functions file. Change those like below:

Parent Theme

function my_scripts() { 
    wp_register_style('bootstrap', get_template_directory_uri() . '/css/bootstrap.css');    
    wp_register_style('my-parent-style', get_stylesheet_uri());        
    wp_enqueue_style('bootstrap');      
    wp_enqueue_style('my-parent-style');
    ....
}

Child Theme

function child_scripts() {  
        wp_enqueue_style( 'my-child-style', get_template_directory_uri(). '/style.css' );     
}

add_action( 'wp_enqueue_scripts', 'child_scripts', 15 );

答案 1 :(得分:0)

In your child theme, replace your code with following codes in functions.php

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