wordpress breadcrumb


/ Published in: PHP
Save to your folder(s)

A custom breadcrumb function for WordPress that I created for a client.

You can pass 2 arguments, the first being the text you want to display in front of the breadcrumb and the second is to hide the home item in the breadcrumb

It will give each item a class and also the last item will receive a class active so you can style it more easily. I always welcome feedback (good or bad)


Copy this code and paste it in your HTML
  1. function the_breadcrumb($before_text = 'You are here:', $home = false) {
  2. $breads = array();
  3. if(is_home() && $home != false){
  4. $breads['home'] = 'Home';
  5. } else {
  6. if (get_post_type() && get_post_type() != 'page' && get_post_type() != 'post' ) {
  7. $breads['post_type'] = get_post_type();
  8. } elseif (get_post_type() == 'post') {
  9. $breads['blog'] = 'Blog';
  10. } elseif (get_post_type() == 'page') {
  11. $breads['page'] = get_the_title();
  12. }
  13. if (is_category() || is_single()) {
  14. foreach((get_the_category()) as $category) {
  15. $breads['category'][] = $category->cat_name;
  16. }
  17. if (is_single()) {
  18. $breads['single'] = get_the_title();
  19. }
  20. }
  21. elseif (is_tag()) { $breads['tags'] = single_tag_title();}
  22. elseif (is_day()) { $breads['archive'] = "Archive for "; the_time('F jS, Y');}
  23. elseif (is_month()) { $breads['archive'] = "Archive for "; the_time('F, Y');}
  24. elseif (is_year()) { $breads['archive'] = "Archive for "; the_time('Y');}
  25. elseif (is_author()) {$breads['author_archive'] = "Author Archive";}
  26. elseif (isset($_GET['paged']) && !empty($_GET['paged'])) {$breads['blog_archive'] = "Blog Archives";}
  27. elseif (is_search()) {$breads['search'] = "Search Results";}
  28. }
  29.  
  30. echo $before_text . ' ';
  31. $breadItems = count($breads);
  32. $i = 0;
  33. $b = '';
  34. foreach ($breads as $key => $bread) {
  35. $i++;
  36. if ($i == $breadItems) {
  37. $active = 'active';
  38. }
  39. $b .= '<span class="bread_item bread_item_' . $key . ' ' . $active . '">';
  40. if ($key == 'home') {
  41. $b .= '<a href="' . get_option('home') . '">' . $bread . '</a>';
  42. } elseif($key == 'category') {
  43. foreach ($breads['category'] as $cat) {
  44. $cats = $cat . '/ ';
  45. }
  46. $b .= rtrim($cats, '/ ');
  47. } else {
  48. $b .= $bread;
  49. }
  50. $b .= '</span> &raquo; ';
  51. }
  52. echo rtrim($b, ' &raquo; ');
  53. }

Report this snippet


Comments

RSS Icon Subscribe to comments

You need to login to post a comment.