Wordpress Get Current Page Template

Posted onby admin

Sometimes in Wordpress development you will need to display certain theme files, using the rewrite engine. For example using custom post types will use a file name single-{posttype}.php, but if you are having rewrite problems Wordpress might just be displaying the single.php file. If you have multiple post types which using similar styling it can be quite confusing on which theme file Wordpress is actually using. Add the below snippet into your functions.php file and it will return the current theme file Wordpress is using.

Get WordPress; Codex. Check out the new WordPress Code Reference! Function Reference/get page templates. Current events; Recent changes; Random. Another wonderful day is happening! Yes, even in the COVID-19 epidemic. Okay, in today’s tutorial I am going to show you how easily we can add page template from WordPress plugin. Interested in functions, hooks, classes, or methods? Check out the new WordPress Code Reference! You can find your profile page is in your WordPress admin - Users - Profile. I don’t see the current template file name in the toolbar. Do you logged in as “Administrator”? Other role users like “Editor” can’t see the file name. If your WordPress is multisite, only super admin can see the file name.

Now on your header.php file you just need to echo the global variable in this function.

Check The Page Template

If you want to make sure you only add code when a certain page template is being used then you can check the current page template by using the WordPress function is_page_template(). This function takes one parameter this is the file of the page template, if the current page template matches this parameter then the function will return true.

Wordpress Get Current Page Template Download

Checking The Page Meta Data

Another way of getting the page template that your currently using is to access the post meta data in the database which stores which template to use. If you are using a custom page template then when you save the page the template will be stored in the wp_postmeta table under the meta name _wp_page_template. To access this data you just need to use the function get_post_meta();

Sometimes we want to get current page title, which is displayed in <title> tag. Here I mean the page in general meaning, not WordPress “page”. We have several WordPress functions that can help such as:

  • get_the_title(): to get current post/page/any post type title
  • single_cat_title(): to get current category title. There’re sibling functions for tags and terms as well: single_tag_title() and single_term_title()
  • get_bloginfo(): to get the blog name
  • etc.
Wordpress page template tutorial

Using those functions are great, but they have some disadvantages:

  • Those functions are specific for particular pages like single post/page, category/tag, … pages. But we can have many page types in WordPress like: 404 page, search result page, archive page, or even virtual page.
  • The title displayed differently in themes: you may use a theme that have title in format Post Title Blog Name, but another theme can use Post Title only. You can make a combination of the functions above to achieve the correct title, but that won’t be used widely for other themes.
  • Or you might set the title in various ways with SEO plugins
Template

In this post, I’m gonna to use a simple trick that can solve these problems quickly.

Wordpress Get Current Page Template

Wordpress Edit Page Template

I’m sure all of us are familiar with function wp_title(), we often use this function in header.php file, like this:

Wordpress Get Current Page Template Name

to display the page title (you should use only this function to display page title, otherwise your site maybe not compatible with SEO plugins or causes performance issue).

GetOnline

The trick is switch the 2nd parameter $echo from true to false to get the value of current page title:

How To Make Wordpress Template

Here is an example where this technique is useful. We’re going to display a Twitter button. I’m gonna to use another cool technique to get the current page URL:

Wordpress Get Current Page Template Email

I’m currently use the script above (with some additions for Facebook like, Google+ buttons) for my websites. And it’s working great in archive pages where built in WordPress functions aren’t very helpful.