WordPress Development Stack Exchange is a question and answer site for WordPress developers and administrators. Join them; it only takes a minute:

Sign up
Here's how it works:
  1. Anybody can ask a question
  2. Anybody can answer
  3. The best answers are voted up and rise to the top

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?

Thank you!

share|improve this question
up vote 3 down vote accepted

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() ;
share|improve this answer
The debug_backtrace does not seem to take into account some of WordPress's idiosyncrasies ( so it does not actually tell you all files that are called) but it will tell you the current template file. Also the above function can be shortened by simply putting debug_print_backtrace(); in your footer wrapped in pre tags. – Wyck May 12 '12 at 5:53
There's a number of files missing, but template-wise (if called in footer.php), it calls everything but header.php. That's not perfect, but I'm not really sure if it's possible to get a full backtrace in WP. The main reason I use this one is because I like having the <hr /> between the lines - personal preference. – SickHippie May 14 '12 at 17:21
Added a more functional full backtrace per @kaiser in WPSE Chat, generates all files used from start to finish. – SickHippie May 14 '12 at 17:59
Not that I care much in this case, but I'd please you to not update answers here with stuff I give you in chat without asking before. This site is under a license, where code that's shared on chat not is. – kaiser May 14 '12 at 19:56

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 . '-->';
share|improve this answer

Your Answer


By posting your answer, you agree to the privacy policy and terms of service.

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