Posted By

jamalorg on 08/22/11


Tagged

wordpress tags conditional ispagetemplate


Versions (?)

Conditional WordPress function to detect page templates when the page is used to display posts, not page content.


 / 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
}
  1. /**
  2.  * is_custom_page_template()
  3.  *
  4.  * This is used to test the current page template when the page template in question is used to display
  5.  * posts (post archives), not regular page content. The default conditional tag is_page_template() fails
  6.  * in this situation, so we're using this in-house function instead.
  7.  *
  8.  *
  9.  * @since 0.1.0
  10.  *
  11.  * @global $wp_query WordPress query object.
  12.  * @param string $template Page template file name (e.g. page-front.php)
  13.  * @return bool Returns false if page template was not found.
  14.  */
  15. function is_custom_page_template( $template = '' ) {
  16. global $wp_query;
  17.  
  18. $page_id = $wp_query->get_queried_object_id();
  19.  
  20. if ( $template == get_post_meta( $page_id, '_wp_page_template', true ) )
  21. return true;
  22. else
  23. return false;
  24. }

Report this snippet  

You need to login to post a comment.