Wordpress child theme style.css not working

前端 未结 5 1898
[愿得一人]
[愿得一人] 2021-02-01 21:34

I have created a file structure in the same format as my parent theme. My parent theme is called Alpine and within Alpine there is a functions.php and style.css file. There do n

相关标签:
5条回答
  • 2021-02-01 21:43

    You need to enqueue the child theme style.css so your function should be:

    function my_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 ),
            wp_get_theme()->get('Version')
        );
    }
    add_action( 'wp_enqueue_scripts', 'my_theme_enqueue_styles' );
    

    Take a look at the documentation.

    0 讨论(0)
  • 2021-02-01 21:51

    This worked for me:

    <?php
    function my_theme_enqueue_styles() {
        $parent_style = 'twentyseventeen-style'; 
        $child_style = 'twentyseventeen-child-style';
        wp_enqueue_style( $parent_style, get_template_directory_uri() . '/style.css' );
        wp_enqueue_style( $child_style, get_stylesheet_uri() );
    }
    ?>
    

    and none of the other answers did.

    0 讨论(0)
  • 2021-02-01 21:52

    Take a look at your <head> tag. More importantly look at the order of your stylesheets.

    The styles from your child theme are being added first and then all the styles from your parent theme. This will cause the styles from the parent theme to override your child theme styles.

    You can change the priority of your my_theme_enqueue_styles function to run after the parent by using the third parameter of add_action. This will enqueue your child theme styles last and allow the CSS to work as expected.

    <?php
    add_action( 'wp_enqueue_scripts', 'my_theme_enqueue_styles', 11 );
    function my_theme_enqueue_styles() {
        wp_enqueue_style( 'child-style', get_stylesheet_uri() );
    }
    ?>
    
    0 讨论(0)
  • 2021-02-01 22:02

    Craig Hick's answer worked for me and I also realize why my default code from the Wordpress documentation wasn't working.

      <?php 
    add_action( 'wp_enqueue_scripts', 'my_theme_enqueue_styles' ); 
    function my_theme_enqueue_styles() {
    
        $parent_style = 'parent-style'; // This is 'twentyfifteen-style' for the Twenty Fifteen theme.
    
        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 my case, the line wp_get_theme()->get('Version') returned 1.0.0 which was the same version number as the parent theme. So I just changed the child theme css version number to 1.0.1 and it worked.

    0 讨论(0)
  • 2021-02-01 22:04

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

    0 讨论(0)
提交回复
热议问题