I am attempting to compare a username to a page template name. I found the following code:
get_post_meta( $post->ID, '_wp_page_template', true );
But this isn’t quite what I am looking for. This returns the filename of the page template, IE:
templates/page-products.php
What I am looking for is the actual template name defined in the page template here:
/*
Template Name: products
*/
Is this possible?
2 Answers
An alternative to @Ben Cole that’s less intensive (especially if you have several page templates), but not as awesome because it doesn’t use the WP_Theme
object 😉
function wpse_184317_get_template_name( $page_id = null ) {
if ( ! $template = get_page_template_slug( $page_id ) )
return;
if ( ! $file = locate_template( $template ) )
return;
$data = get_file_data(
$file,
array(
'Name' => 'Template Name',
)
);
return $data['Name'];
}
And in use:
echo wpse_184317_get_template_name(); // Template name for current page
echo wpse_184317_get_template_name( 14 ); // Template name for page ID 14