Site logo

[Resolved] determining page template

Home Forums Support [Resolved] determining page template

Home Forums Support determining page template

Viewing 6 posts - 1 through 6 (of 6 total)
  • Author
    Posts
  • #2200845
    eric

    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’ )) {}

    #2200855
    David
    Staff
    Customer Support

    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
    }
    #2200870
    eric

    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.”

    #2200874
    David
    Staff
    Customer Support

    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
    }
    #2203359
    eric

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

    #2203601
    David
    Staff
    Customer Support

    Glad to hear that!

Viewing 6 posts - 1 through 6 (of 6 total)
  • You must be logged in to reply to this topic.