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?
Answer
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:
|
1 |
$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 template files are generating the page, use this in your functions.php:
|
1 2 3 4 5 6 7 8 9 10 |
// Returns a list of files used to generate the page.
function wpse51906_list_template_names() {
echo '<pre>';
foreach ( debug_backtrace() as $called_file ) {
print_r( $called_file['file'] );
echo '<hr />';
}
echo '</pre>';
}
add_action( 'shutdown', 'wpse51906_list_template_names' ); |


