0

I'm building a child theme of Newspaper 7, and I'm trying to call a specific stylesheet for certain pages, however is_page() isn't working for me. What should I change in my code to be able to do what I'm trying to do?

Here is my code:

<?php
add_action( 'wp_enqueue_scripts', 'theme_enqueue_styles', 1001);
function theme_enqueue_styles() {
    wp_enqueue_style('td-theme', get_template_directory_uri() . '/style.css', '', TD_THEME_VERSION . 'c' , 'all' );
    wp_enqueue_style('td-theme-child', get_stylesheet_directory_uri() . '/style.css', array('td-theme'), TD_THEME_VERSION . 'c', 'all' );

if( is_page( array( 'about-me', 'contact', 'social-media'))) {

    wp_enqueue_style( 'td-theme-child', get_stylesheet_directory_uri().'/style2.css'. 'c', 'all' );
}

}
1
  • The first thing that jumps out is there are two stylesheets with the same slug. Try changing the name of the slug on the one wrapped in the is_page() conditional to something unique. Also '/style2.css'. 'c', looks like a typo to me... Mar 28 '17 at 21:01
0

The first parameter of wp_enqueue_style and wp_register_style is used to register the stylesheet for later use. So, you can register it early and later call it just specifying it's first parameter.

In this case you are registering and enqueueing the stylesheet td-theme-child, which file is style.css. After that, if the conditional passes, you are registering and enqueueing the stylesheet td-theme-child, which file is style2.css... but, WP already sent the td-theme-child, so the style2.css will never hit the browser.

So, in short, just change the first parameter of your style2.css enqueue line:

wp_enqueue_style( 'td-theme-child2', get_stylesheet_directory_uri().'/style2.css'. 'c', 'all' );
1
  • I tried this and it worked! Thank you for helping and explaining. Mar 29 '17 at 13:36

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.