Another way to load the parent theme style

WordPress twenty-one 3.6K Reading mode

Sometimes for personalized themes, we will create sub themes to facilitate the modification of theme function and appearance without modifying the original theme. Usually, we will use:

  1. @import  url( '../parent-theme-folder/style.css' );

This loads the style of the parent theme.

See:

However, using the @ import method to load styles also has some disadvantages, such as slow loading.

We can use another method to load the parent theme style:

  1. add_action( 'wp_enqueue_scripts', 'my_parent_theme_css' );
  2. function  my_parent_theme_css() {
  3.     wp_enqueue_style( 'parent-style', get_template_directory_uri() .  '/style.css' );
  4.     wp_enqueue_style( 'child-style', get_stylesheet_uri(),  array ( 'parent-style' ) );
  5. }

Add code to the subtopic functions.php template file.

It is also possible to further:

  1. add_action( 'wp_enqueue_scripts', 'my_enqueue_styles' );
  2. function  my_enqueue_styles() {
  3.      /* If using a child theme, auto-load the parent theme style.  */
  4.      if  ( is_child_theme() ) {
  5.         wp_enqueue_style( 'parent-style', trailingslashit( get_template_directory_uri() ) .  'style.css' );
  6.     }
  7.      /* Always load active theme's style.css.  */
  8.     wp_enqueue_style( 'style', get_stylesheet_uri() );
  9. }

In some special cases, you may not want to load the style of the parent theme. You can write as follows:

  1. add_action( 'wp_enqueue_scripts', 'my_dequeue_styles', 11 );
  2. function  my_dequeue_styles() {
  3.     wp_dequeue_style( 'parent-style' );
  4. }

Original text: http://justintadlock.com/archives/2014/11/03/loading-parent-styles-for-child-themes

 

Most of the articles on this site are original and used for personal learning records, which may be helpful to you, for reference only!

 weinxin
My Wechat
Copyright Notice
Please indicate the source and link of the original article reprinted on this site. Thank you for your cooperation!
five hundred and ninety-eight million eight hundred and forty-five thousand and six
 
 Robin
five hundred and ninety-eight million eight hundred and forty-five thousand and six
Comments twenty-one    Visitors twenty    Author one
    •  Boke112 navigation
      Boke112 navigation five

      I used to like to use sub themes, but now I like to modify them directly, which is easier

      •  Fuck you
        Fuck you zero

        I used your theme before, with the support of
        Change the link www.caotama.com Fuck you

        •  Investment and financing records
          Investment and financing records five

          Brother Bird imitates the style of Baidu Baijia.

          •  March welfare bar
            March welfare bar two

            Collect first~~~~

            •  Lushanshen
              Lushanshen one

              support

              •  Hong Kong server
                Hong Kong server three

                I think it is more direct to change the theme directly, and it is also easy to do. If I make sub themes, I feel particularly troublesome. Maybe I become lazy

                •  Ali Books
                  Ali Books five

                  Good way

                  •  Ouch, hello
                    Ouch, hello zero

                    There is a very tangled problem. I can't find a solution. Please ask me: My website has set up a group of antithetical advertisements, which display OK on the PC, but the mobile phone will block the page content, so I don't want this advertisement to display on the mobile phone.

                    •  Lao Dong's Blog
                      Lao Dong's Blog zero

                      I still like simple themes, but I often see that overseas WP themes operate like this.

                      •  Learned
                        Learned one

                        Learned

                        •  Ningbo PDM
                          Ningbo PDM zero

                          I prefer to read similar blogs. It's really good

                          •  Less traffic marketing
                            Less traffic marketing two

                            I only want to use one theme for the time being.

                            •  wo
                              wo zero

                              Can you provide XML for your theme? It's not easy to set it

                              •  The watch has exploded!
                                The watch has exploded! three

                                Maxian

                                •  Discover special tours
                                  Discover special tours zero

                                  It's just time to use it. Thanks for learning!

                                  •  WeChat Group Anthology
                                    WeChat Group Anthology zero

                                    The theme is beautiful and simple

                                    •  Xincheng
                                      Xincheng one

                                      .. Sofa

                                     anonymous

                                    Comment

                                    Anonymous netizens
                                     :?:  :razz:  :sad:  :evil:  :!:  :smile:  :oops:  :grin:  :eek:  :shock:  :???:  :cool:  :lol:  :mad:  :twisted:  :roll:  :wink:  :idea:  :arrow:  :neutral:  :cry:  :mrgreen:

                                    Drag the slider to complete validation