One of the many great features of Genesis is the built-in breadcrumbs. No plugin is required, and they’re VERY customizable. Of course there’s the Breadcrumb toggles in Genesis > Theme Settings that let you specify where they show up. But you can do a lot more with code.
If you look in /genesis/lib/classes/breadcrumb.php you’ll see all the code used for the breadcrumbs. For each instance of a breadcrumb (single, archive, home, blog, page…) there’s a function for it and a filter to modify that function’s output.
To modify the breadcrumbs, just pick the filters you want to use and write a function that does something with the $crumb.
In this example, I’ll add “Blog” before “Category” on the category and single breadcrumbs. By default, the breadcrumb renders this:
- On blog home: Home > Blog
- On category: Home > Category
- On single post: Home > Category > Post Title
Here’s how I do it:
The bottom two lines are the filters I want to modify (single and archive). In my function I’m using get_option('page_for_posts')
to get the ID of the blog homepage. I’m then using the core WordPress functions get_permalink()
and get_the_title()
to return the permalink and title of this page ID.
This line ( if ( is_singular( 'post' ) || is_category() )
) ensures this only applies to blog posts and post categories. The filters are also used for posts in custom post types and taxonomies.
Arya says
Hi Bill,
I don’t know whats wrong but when I try the code it return the post name before category.
http://bit.ly/tjq40J
Do I miss something?
Bill Erickson says
Can you post the exact code you’re using? It looks like you’re using
get_permalink()
andget_the_title()
without addingget_option( 'page_for_posts' )
inside it.Arya says
I have try to copy and paste again from your snippet but still same.
Bill Erickson says
It sounds like the issue is that you’re using the homepage for your blog. This code only works (and is only needed) if you’re using a custom page for your blog, set in Settings > Reading. When your blog is your homepage, the Home link goes to your blog already.
Arya says
Ok, got it now. Is work after set in Settings > Reading. Thanks a lot 🙂
Candace says
This works great except on the monthly archive pages:
http://alumni.deserthigh.com/blog/2012/03
How would I fix that?
Thanks!
Bill Erickson says
You can add is_date() to the list of conditionals to apply it to date archives too: https://gist.github.com/2141490
Candace says
Bill,
That worked! Thank you soooo much!!
Bhaskar Sarma says
Bill, does this method still work after the latest update to Genesis and Prose? I followed the code but it seems to be no -go.
Bill Erickson says
Yes it does since the latest version didn’t change the Breadcrumbs. Genesis is built to be very backwards compatible, so updating it should never break existing functionality like this.
Paul says
Thanks Bill, this worked great. Just one thing though, how would I get the breadcrumb to not show the category name on posts? (As the url and breadcrumb don’t match)
Currently shows:
Home / Blog / {category} / {name of post}
Want to show:
Home / Blog / {name of post}
Any help would be much appreciated!
Bill Erickson says
Try this instead: https://gist.github.com/3776142
The code in the tutorial simply adds “Blog” to the beginning of the current breadcrumb. In the code I provided above, we’re also changing the breadcrumb on single posts to just be the post title (and then after that, the Blog part is added to the beginning)
Paul says
Worked like a charm, thanks Bill.
Steve Struemph says
Thank you Bill! This is exactly what I needed 🙂
Muhammad Usama says
Thank you Bill! You are great.
Chris says
Bill, this is still super-useful after all these years. Thank you!
David Foster says
Hi Bill, sorry to trouble you but, in which file is this code added? I’m using an Imagely child theme which does not have a breadcrumb.php file and I’m loathe to edit the one in the Genesis directory.
Thanks and best wishes
David
Bill Erickson says
You can add it to your child theme’s functions.php file.
Omscowonder says
You are simply the best.
i just added the breadcrumbs to my genesis child themes, it works like a charm
Jake says
Is there a way to make the changes be applied to categorized posts and not all singular posts?
Bill Erickson says
Remove the add_filter( ‘genesis_single_crumb’ )… line, since that code applies it to the single posts.
It’s also a good idea to remove the is_singular() conditional. Even though this code doesn’t run on single posts once you make the above change, for clarity all references to the single post should be removed.
Matt says
Bill,
How would I modify this code to display breadcrumbs on my custom posts archive pages? Thanks very much.
Matt
Kevin says
Any clue how to get blog in the URL? When I click blog it has site.com/blog but when it goes to a single page it’s just site.com/blog_title
I had this working a ways back but lost the snippet : / and am thumping my head on the desk now! Thanks!
Bill Erickson says
Go to Settings > Permalinks, select “Custom” and then type /blog/ . That will add /blog/ to the front of all the single post permalinks.
Kevin says
Yup figured that part out but all my custom post types and taxonomies had /blog/ placed in front of their URLs as well, after some research I added the below code when registering CPT’s and TAXs and it starts at the root URL removing blog, thanks for the reply as always and if you have a better approach I am all ears. Hope that’s not too much code to include:
Front set to false being the key bit here.
‘rewrite’ => array( ‘slug’ => ‘promotions/’, ‘with_front’ => false),
Kingsley says
Nice one but am kinda confuse