How do I find if a page has a template?

So I’m doing a query like this:

    $the_query = new WP_Query( array( 'meta_key' => 'homepage', 'meta_value' => 'yes', 
'post_type' => 'page', 'orderby' => 'modified', 'posts_per_page' => 1 ) );

To get a single page with a specific key value, how do I get the page template from a query like this, if it has one?

Read More

Thank you!

Related posts

Leave a Reply

2 comments

  1. This should do the trick for you. This shows what template file is stored in post_meta, if one has been selected in the admin panel:

    $template_name = get_post_meta( $the_query->post->ID, '_wp_page_template', true );

    If you want to see if the page is the homepage, use is_home() or is_front_page().

    If you want to see what files are generating the page, use this in your functions.php:

    // Returns a list of files used to generate the page.  Best called in footer.php before </body>
    function _dump_files()
    {
        # @todo Aufrufende Datei kann im Array manchmal fehlen!
        add_action( 'all', create_function( '', "echo '<pre>'; print_r( get_included_files() ); echo '</pre>'; return;" ) );
    }
    

    I use it in footer.php like this:

    if (is_user_logged_in()) {
        _dump_files() ;
    }
    
  2. One quick method I use is the WordPress global $template. I output it in the page source as well.

    Just before the closing </body> tag
    global $template;
    echo '<!-- the template is:' . $template . '-->';

    or as a function:

    add_action('wp_footer', 'show_template');
    function show_template() {
        global $template;
        echo '<!-- the template is:' . $template . '-->';
    }