Yesterday I needed to selectively hide breadcrumbs for particular WordPress templates, but had some issues with getting the WordPress current template name when needed, so I thought I’d share how I went about it.
First things first – the way you go about it depends on whether you’re working with pages or posts – these are not seen as the same thing by WordPress, and it can cause some confusion. Posts include any custom post types that you may have created, by the way.
Getting the WordPress current template name for PAGES
Ordinarily when targeting a specific page, you can use is_page();
if ( is_page(*put_your_id_here*) ) { // Returns true when the current page is *put_your_id_here* } else { // Returns false when the current page is *put_your_id_here* }
The problem is, this requires you to put in a specific page_id. What happens if you use the same page template for multiple pages, though? You don’t want to be adding to a never ending list of IDs when you add a new page with that template, so it’s necessary to find a solution that applies to all.
A quick browse through some other code I remembered working with before brought me to the is_page_template() function;
if ( is_page_template('about.php') ) { // Returns true when 'about.php' is being used. } else { // Returns false when 'about.php' is not being used. }
Perfect – simply put your page template file name (including the .php) into the above, and if the function returns true, I don’t output the breadcrumbs.
Getting the WordPress current template name for POSTS
As mentioned above – don’t forget that this includes custom post types you create – they’re all classed as posts by WordPress.
The post version of is_page() is is_single();
if ( is_single(*put_your_id_here*) ) { // Returns true when the current post is *put_your_id_here* } else { // Returns false when the current post is *put_your_id_here* }
Update: you can also use is_singular() to target a specific custom post type single post.
Again, though, this relies on you targeting a specific post_id. Fine if you only want to target a single post, but useless if you want to target all posts that use the same WordPress template.
In my case, I had a custom post type – Guides, and didn’t want to show the breadcrumbs on any single posts for this post type. As a result, I knew that I needed to target every post that was using the PHP template file single-guide.php – this being the template file I’ve created to override the standard WordPress single post template single.php. If you’re not sure on this – have a read of the WordPress template hierarchy – it explains how you can easily override templates depending on a range of criteria – post types, categories, taxonomies and more.
As it turns out, there’s no easy in-built function to get WordPress to know what template it’s using for posts. The solution I found on StackExchange is to put the following in your theme’s functions.php file:
// Grabbing current page template add_filter( 'template_include', 'var_template_include', 1000 ); function var_template_include( $t ){ $GLOBALS['current_theme_template'] = basename($t); return $t; } function get_current_template( $echo = false ) { if( !isset( $GLOBALS['current_theme_template'] ) ) return false; if( $echo ) echo $GLOBALS['current_theme_template']; else return $GLOBALS['current_theme_template']; }
What this does is;
- grab the WordPress current template name when WordPress pre-processes it before outputting to the screen
- drops this template into a global variable so you can use it throughout the site
All you need to do now is call your new get_current_template() function to your check to see if the current template matches the one you’re looking for, in my case to exclude the breadcrumbs in my theme’s header.php file;
if ( !get_current_template() === 'single-guide.php' ) { output_breadcrumbs(); // don't show crumbs here on guide pages }
Hope that helps someone – if anyone knows of a better way, let me know!
I just had the same problem, trying to dequeue a script when using a custom post type. Thanks for explaining why is_page_template() wouldn’t work, pretty obvious if you think about it but still …
Anyway, I took a different approach:
if ( ‘portfolio’ == get_post_type() )
Where “portfolio” is the custom post type name of course (must be lower case too apparently). This doesn’t return the template name but is functionally the same I believe, only less complex and requires less code …
HI Maarten,
You’re right that the code you attached will tell you that you’re on a particular post type – the problem is that this will be true on a post type archive page as well as a single post, which isn’t what I wanted.
I’ve actually found another WordPress function that specifically checks whether you’re on a single instance of a custom post type:
WordPress codex – is_singular()
In my instance I need to target specific templates elsewhere, so the code I posted above works best for me – though if you’re purely targeting a post type you’re best off using either the code you posted or the code above, depending on what/where you want to output.
That is an even better solution indeed. I already suspected mine wasn’t completely bulletproof so even though it makes no difference in my case (not using an archive for the custom post type), I’m using is_singular() now.
Just a WP beginner here and learned something new again!
Thanks for sharing. Exactly what I was looking for.
This solution helped me greatly! Thank you