0

I am using a child theme. I want no-sidebar.css to be loaded if page-nosidebar.php is loaded. So I wrote the following code. It is not working. But it works in the Parent Theme:

function no_sidebar_style(){
    if (is_page_template('page-templates/page-nosidebar.php')) {
        wp_enqueue_style( 'no-sidebar-style', get_stylesheet_directory_uri().'/css/no-sidebar.css');
    }
}
add_action( 'wp_enqueue_scripts', 'no_sidebar_style', 19);

If I write:

is_page_template(get_stylesheet_directory_uri().'/page-templates/page-nosidebar.php')

instead of:

(is_page_template('page-templates/page-nosidebar.php')

as shown below, it still does not work:

function no_sidebar_style() {
    if (is_page_template(get_stylesheet_directory_uri().'/page-templates/page-nosidebar.php')) {
    wp_enqueue_style( 'no-sidebar-style', get_stylesheet_directory_uri().'/css/no-sidebar.css');
    }
}
add_action( 'wp_enqueue_scripts', 'no_sidebar_style', 19);

What do I need to do to make this code work?

3
  • Your first version of no_sidebar_style() looks fine. Where did you place that code in your child theme? Can you elaborate on 'not working'? Are you getting a 404 on the stylesheet or is nothing happening at all? For debugging, you can use get_page_template() to see the name of the template you're viewing. Sep 19, 2016 at 2:06
  • Hello Dave, You are right, first version is fine. After your guideline. I have done my code with more attention. Now it is Okay. I am facing no trouble. But it was a trouble of 1 week. Many thanks.
    – Zubair
    Sep 19, 2016 at 5:23
  • Happy to help, Zulfikar. I've added my comment as an answer. Can you please accept it to help the next person that might run into this issue? Sep 19, 2016 at 5:31

1 Answer 1

0

Your first version of no_sidebar_style() looks fine:

function no_sidebar_style(){
    if (is_page_template('page-templates/page-nosidebar.php')) {
        wp_enqueue_style( 'no-sidebar-style', get_stylesheet_directory_uri().'/css/no-sidebar.css');
    }
}
add_action( 'wp_enqueue_scripts', 'no_sidebar_style', 19);

For debugging, you can use get_page_template() to see the name of the template you're viewing.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

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