Automatic ‘parent category’ -> ‘custom taxonomy term’ breadcrumb code
-
The code below automatically selects the breadcrumb trail for the category, and custom taxonomy.
The most important bit for me was how to figure out, how to choose a custom taxonomy term automatically, based on the taxonomy term url. As well as how to get the url of just a parent category.
The breadcrumb trail has the custom taxonomy term after a parent category:
Home -> Parent category -> Custom taxonomy term
You might also be able to do it as:
Home -> Custom taxonomy -> Taxonomy term
However when I tried to do it this way and linked to blog.com/custom_taxonomy, the link gave a 404 error. As I am using a different set-up (category -> taxonomy term) in the first place, and doing url rewriting in both the theme and the Apache paths, it is not something I need at the the moment so I did not spent a lot of time trying to figure out. But feel free to do so.
It took me awhile to figure this out, but I hope it helps others on their way.
Please do feel free to improve and customize. This is a beta code snippet. Use at your own risk. I would not use this code snippet on a live site yet. The code is more meant for people trying to figure their way out in coding with automatic category/ custom taxonomies selection and breadcrumbs, and to build from there if and when needed.
Code:
<?php $term = get_term_by( 'slug', get_query_var( 'term' ), get_query_var( 'taxonomy' ) ); $tax_term_breadcrumb_term_slug = $term->slug; $tax_term_breadcrumb_taxonomy_slug = $term->taxonomy; foreach (get_the_category() as $cat) { $parent = get_category($cat->category_parent); $parent_name = $parent->cat_name; $parent_slug = $parent_name; $parent_slug = strtolower(str_replace("(","",$parent_slug)); $parent_slug = str_replace(')',' ', $parent_slug); $parent_slug = str_replace(' ','-', $parent_slug); } if ( is_category($parent_name) ) { ?> <a href="<?php echo get_option('home'); ?>/" title="Home">Home</a> » <? echo $parent_name; ?> <?php } elseif (is_tax($tax_term_breadcrumb_term_slug)) { ?> <a href="<?php echo get_option('home'); ?>/" title="Home">Home</a> » <a href="<? echo get_option('home')?>/<? echo $parent_slug ?>" title="<? echo $parent_name;" ?>"><? echo $parent_name; ?></a> » <?php echo $term->name; ?> <?php } elseif ( is_front_page() ){ ?> Home <? } ?>
Hope it helps. ?? Suggestions and feedback are welcome.
Edit: The forum automatically converted the » in the code above.
The original code was &.#.1.8.7.;. (without the dots) instead of the »
- The topic ‘Automatic ‘parent category’ -> ‘custom taxonomy term’ breadcrumb code’ is closed to new replies.