Checking for page templates in child theme

I have a simple problem I’m hoping someone can shed some light on. I have a child theme with a custom page template and I’m simply trying to check weather or not the template is in use. Under normal circumstances, I would have just used the is_page_template function, however it doesn’t seem to be working with child themes. I’ve tried the following

if(is_page_template('template-custom-fullwidth.php')){
    //do something..
}

as well as

Read More
if(is_page_template(dirname(get_bloginfo('stylesheet_url')).'/template-custom-fullwidth.php'){
    //do something..
}

Neiter works and I was hoping there is a more elegant solution than using $_SERVER to check for URLs. I can’t imagine there not being a function for this seeing as this seems like a common task. I believe the problem is the difference between template and stylesheet directories. Is it possible to use WordPress to check for page templates located in a child theme?

Thanks in advance!

Related posts

Leave a Reply

8 comments

  1. I faced similar issue. One few trial and error noticed that if the conditional is kept inside the function it works. but if kept outside it does not.

    function team_page_enqueue_style() {
    
        if ( is_page_template('page-team.php') ) {
            wp_enqueue_style( 'easy-responsive-tabs-css', get_stylesheet_directory_uri() . '/css/easy-responsive-tabs.css', array(), NULL);
        }
    }
    
    add_action( 'wp_enqueue_scripts', 'team_page_enqueue_style' );
    
  2. Had the same issue and solved it like this:

    get_stylesheet_directory_uri() won’t work cause it will show the url and you need the server full path, use get_stylesheet_directory()

    If is_page_template() doesn’t work you can use get_page_template() and compare

    if(get_page_template() == (get_stylesheet_directory() . '/custom-template.php')){
    //your stuff
    }
    
  3. Try is_singular(); it worked for my case as my template is a single post page template.

    To use it, you need to specify the name of the template without the word single- and .php. For example, if the template file is single-forest_of_trees.php, then this should be the code:

    if (is_singular( 'forest_of_trees' )) {
       // do something
    }
    

    it also allows an elegant way for multiple values.

  4. The only thing that works for me in a child theme is accessing the global $template variable and comparing against it:

    global $template;
    if (basename($template) === 'template-custom-fullwidth.php') {
       // do something
    }
    
  5. Use a relative path from your theme’s folder:

    if(is_page_template('page-templates/template-custom-fullwidth.php')){
    //do something..
    }