I just ran into a problem with the WordPress Breadcrumbs plugin included in WordPress SEO by Yoast and I thought I share it here. The problem I encountered was with the custom post types not displaying in the breadcrumbs trail if you use the custom post type archive page instead of a page with a custom template. So I’ve created a Yoast Breadcrumbs custom post type fix to display custom post types in the breadcrumbs trail when viewing post type archives.
I have a Custom Post Type called “Events” which has a slug named “events”. I wanted to show the EVENTS loop (custom post type archive) without having to create a page called “Events” and use a custom template for it to display a loop with the custom post types. Usually the page you will create for displaying a custom post type loop will conflict with the custom post type slug (the page will have the slug events-2 or something or vice-versa) so that’s why I wanted to do this as WordPress intended for us to use.
After I’ve created links in my menu to reflect the archive page for the events, pointing to http://mydomain.com/events, I assumed it will work as expected. For my disappointment the Breadcrumbs plugin didn’t display the title of the archive pages, or the post type name in the breadcrumb trail.
I had to fix it somehow, keeping in mind that I had to use this fix with multiple custom post types. Here’s a quick fix for this if you are using the above mentioned plugins on your project.
Put the following code in your template files where you have the breadcrumbs displayed:
<?php if ( function_exists('yoast_breadcrumb') ) : $post_types = array('events','custom_post_type_2','custom_post_type_3'); if(is_post_type_archive( $post_types )): ?> <div class="breadcrumbs"> You are here: <span xmlns:v="http://rdf.data-vocabulary.org/#"> <span typeof="v:Breadcrumb"><a href="<?php echo home_url(); ?>/" rel="v:url" property="v:title">Home</a></span> » <span typeof="v:Breadcrumb"><strong class="breadcrumb_last" property="v:title"><?php post_type_archive_title(); ?></strong></span> </span> </div> <?php else : yoast_breadcrumb('<div class="breadcrumbs">You are here: ','</div>'); endif; endif; ?>
To explain the code a bit:
- I’m checking to see if the plugin function exists
- I’m creating an Array of custom post types I want to apply this fix to
$post_types = array('events','custom_post_type_2','custom_post_type_3');
- I’m checking to see if we are on a post-type archive page which matches any of the above defined custom post types
if(is_post_type_archive( $post_types ))
- If I’m on a post-type-archive page, I will display the HTML code structure generated by the Breadcrumbs plugin and add the dynamic title of the current post type
- Else, if I’m not on a custom post type archive page, the plugin should work as expected so I’m using the default plugin functionality.
I hope it helps many of you narrow down the time spent on searching for a fix for this.Share