Determine which template-{slug}.php is being loaded

I am trying to make a personal plugin that does specific things based on what is loaded. In this example, in my action method I want to perform one function if the currently loading page is using the template-blog.php template, if it’s a single blog post (loading single.php do a second function, anything else do a 3rd function

I am not quite sure what method I need to use, and so far Google is not pulling up the right methods. I have been trying get_current_template(), get_template() get_page_template(), but none of them pull the stuff I am looking for above.

Answers:

Thank you for visiting the Q&A section on Magenaut. Please note that all the answers may not help you solve the issue immediately. So please treat them as advisements. If you found the post helpful (or not), leave a comment & I’ll get back to you as soon as possible.

Method 1

Using body_class() on the body tag would probably be the easiest way to tell which template is in use.

<body <?php body_class(); ?>>

Depending on which page you are on, it will output similar to this

<body class="page page-id-10 page-template-default logged-in">

Where page-template-default is the template in use. It’s also useful for blog posts and custom post types.


All methods was sourced from stackoverflow.com or stackexchange.com, is licensed under cc by-sa 2.5, cc by-sa 3.0 and cc by-sa 4.0

0 0 votes
Article Rating
Subscribe
Notify of
guest

0 Comments
Oldest
Newest Most Voted
Inline Feedbacks
View all comments
0
Would love your thoughts, please comment.x
()
x