/ Published in: PHP
is_page_template() returns false when the page in question was set to display blog posts via Admin -> Settings -> Reading -> [Posts page]. I've wasted few good hours figuring out what's going on, but finally, I've come up with this function which worked for me.
To use the conditional code, paste the code below in your theme's functions.php or in your plugin functions file and use it like this:
if ( is_custom_page_template( 'template-name.php' ) ) {
// do something
}
Expand |
Embed | Plain Text
/** * is_custom_page_template() * * This is used to test the current page template when the page template in question is used to display * posts (post archives), not regular page content. The default conditional tag is_page_template() fails * in this situation, so we're using this in-house function instead. * * * @since 0.1.0 * * @global $wp_query WordPress query object. * @param string $template Page template file name (e.g. page-front.php) * @return bool Returns false if page template was not found. */ function is_custom_page_template( $template = '' ) { global $wp_query; $page_id = $wp_query->get_queried_object_id(); if ( $template == get_post_meta( $page_id, '_wp_page_template', true ) ) return true; else return false; }
You need to login to post a comment.
