WordPress.org

Make WordPress Core

Opened 4 years ago

Closed 4 years ago

Last modified 2 years ago

#33113 closed enhancement (wontfix)

Add is_template_part() functionality

Reported by: GunGeekATX Owned by:
Milestone: Priority: normal
Severity: normal Version:
Component: Themes Keywords:
Focuses: Cc:
PR Number:

Description

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 ' &hellip; <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.

Change History (4)

#1 @wonderboymusic
4 years ago

  • Keywords close added
  • Type changed from feature request to enhancement

Right now, you can use doing_action( "get_template_part_partials/hero-template" )

#2 @DrewAPicture
4 years ago

  • Keywords close removed
  • Milestone Awaiting Review deleted
  • Resolution set to wontfix
  • Status changed from new to closed

I can't say that I've ever seen another request for this, and there doesn't seem to be much extra interest here. And, as @wonderboymusic mentioned, you can already do this with doing_action().

I'm going to go ahead and say wontfix.

#3 @GunGeekATX
4 years ago

Core does call do_action( "get_template_part_{$slug}", $slug, $name );, but checking for that action as outlined above doesn't work.

In this case, I have a template partials/blog_landing/author-link.php calling a filter:
$link_text = apply_filters( 'blog-landing-author-link-text', get_the_author() );

Inside the function for that filter, when I call
doing_action( 'get_template_part_partials/blog_landing/author');, it always returns false.

If I var_dump() the global $wp_current_filter variable being used by core's doing_action() and doing_filter() functions, it only has one item in the array, the blog-landing-author-link-text filter that is currently running.

Last edited 4 years ago by GunGeekATX (previous) (diff)

#4 @ztig
2 years ago

i would like this to be a possibility as well.

Note: See TracTickets for help on using tickets.