[wp-trac] [WordPress Trac] #33113: Add is_template_part() functionality

WordPress Trac noreply at wordpress.org
Fri Jul 24 16:15:45 UTC 2015


#33113: Add is_template_part() functionality
-----------------------------+-----------------------------
 Reporter:  GunGeekATX       |      Owner:
     Type:  feature request  |     Status:  new
 Priority:  normal           |  Milestone:  Awaiting Review
Component:  Themes           |    Version:
 Severity:  normal           |   Keywords:
  Focuses:                   |
-----------------------------+-----------------------------
 Similar to is_page_template(), this would allow a piece of code to
 determine if it was running inside a partial template.  My guess is we'd
 have to set a global in load_template() right before the require calls,
 then clear it after it's done.
 https://core.trac.wordpress.org/browser/tags/4.2.2/src/wp-
 includes/template.php#L494

 Something like this:
 {{{
 global $wp_template_part;
 $wp_template_part = $_template_file;

 if ( $require_once )
         require_once( $_template_file );
 else
         require( $_template_file );

 $wp_template_part = null;
 }}}

 {{{
 function is_template_part( $template_file ) {
         global $wp_template_part;
         return $template_file === $wp_template_part;
 }
 }}}

 Example of use in a filter
 {{{
 function excerpt_more_filter( $excerpt_more ) {
         if ( is_template_part( 'partials/hero-template.php' ) ) {
                 return ' … ';
         } else {
                 return ' … <a href="' . get_permalink( get_the_id()
 ) . '">Read More</a>';
         }
 }
 }}}

 I had need of altering the return value of a filter slightly based on the
 partial template it was being called from.  I ended up implementing it
 using a global set inside my partial template, but it would be nice to
 have this in core.

--
Ticket URL: <https://core.trac.wordpress.org/ticket/33113>
WordPress Trac <https://core.trac.wordpress.org/>
WordPress publishing platform


More information about the wp-trac mailing list