Get Template File Being Used on Current Page

[ad_1]

Have you ever wanted to get template file being used on a current page? Well, I did, on a project I was working for a client. I wanted to get page template name to filter the post query based on the page template file being used. Although I could have done this by creating few template files for each page that I wanted to modify page query on, but it was not a perfect solution for the problem as well as not convenient if you have large number of pages on your website. I had to find a better way to do that to make sure it will also work on pages created in future too.

Lucky for me, WordPress automatically creates a meta key named “_wp_page_template” for each page that we create in WordPress and assign PHP template file name as its value. With this value we can easily get the name of the page template used for current page.

Here is a little code snippet that you can use in your theme files to return the name of the current page template being used. If you did not assign a template on a page then this meta key will return “default” as in default page template (page.php) is being used.

<?php
    // get template file being used on current page
    echo get_post_meta( $post->ID, '_wp_page_template', true );
?>
[ad_2]

Leave a Reply

Random Post Selection ::

Recent Posts

Unlock Unlimited Downloads