Site logo

Archived topic

determining page template

5 replies · Started by eric on April 26, 2022

Viewing posts 1–6 of 6

Hi there,

I've successfully followed the instructions at https://docs.generatepress.com/article/setting-up-a-simple-custom-post-type/ to create my own templates through GeneratePress.

The problem is that I'm trying to write a script to see if that template is being used. This command doesn't work. Thank you in advance.

(is_page_template('templatename.php' )) {}

Hi there,

is_page_template only applies to a Custom Page Template.
Instead you can check for the post type like so:

if ( 'your_cpt_slug' === get_post_type() ) {
        // do stuff
}

That will apply singles and archives, so if you want to just check the single post then do this:

if ( 'your_cpt_slug' === get_post_type() && is_singular() ) {
        // do stuff
}

Thank you. I'm not able to get this to work.

My template file is called single-templateB-fullwidth.php and the comments I have on the top is this.

/*
Template Name: TemplateB Full Width
*/

I'm trying to enqueue a style and my if statement is supposed to detect if this template is used. In testing this command, get_post_type pulls up a blank space on function.php and on the template file itself it pulls up "page."

Ok - some crossed wires there :)

Are you selecting the Page Template from the page editor?
If you're not then try the global $template variable:

global $template;
if ( basename( $template ) === 'single-templateB-fullwidth.php' ) {
     // do stuff
}

Thank you. I was able to get this to work!

Glad to hear that!

This archived topic is closed to new replies.