Reputation: 61
Is there any way to get the name of the page template?
I mean in a page template, at the beginning we write Template Name: template name
I want "template name
" by page-id
I've tried get_post_meta(get_the_ID(), '_wp_page_template', true) and get_page_template_slug($post_id)
but none of the function helped
Upvotes: 5
Views: 15135
Reputation: 125
There is a bit more proper way to do that. Saying we have already template path:
$template_path = get_post_meta(get_the_ID(), '_wp_page_template', true);
Then we just do this:
$templates = wp_get_theme()->get_page_templates();
echo $templates[$template_path];
it outputs template name. as simple as that.
Upvotes: 6
Reputation: 61
I have figured it out.
As I'm going to use inside WordPress admin
I've used regular expression.
It's something like this preg_match('/\bTemplate Name: [a-zA-Z0-9\s\-_]+\b/',$file,$template);
.
$file
is the file name from get_post_meta(get_the_ID(), '_wp_page_template', true)
It gives me Template Name: template name
and then I used some array functions to get an actual part from returned array from preg_match
.
I hope this solution will help other developers in future.
Upvotes: 0
Reputation: 122
Try to add this code to function.php
add_action('wp_head', 'show_template');
function show_template() {
global $template;
echo basename($template);
}
Let me know.
Upvotes: 0