Wordpress breadcrumbs


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

Originally written by Dimox


Copy this code and paste it in your HTML
  1. function dimox_breadcrumbs()
  2. {
  3.  
  4. $delimiter = '»';
  5. $name = 'Home'; //text for the 'Home' link
  6. $currentBefore = '<span class="current">';
  7. $currentAfter = '</span>';
  8.  
  9. if ( !is_home() && !is_front_page() || is_paged() ) {
  10.  
  11. echo '<div id="crumbs">';
  12.  
  13. global $post;
  14. $home = get_bloginfo('url');
  15. echo '<a href="' . $home . '">' . $name . '</a> ' . $delimiter . ' ';
  16.  
  17. if ( is_category() ) {
  18. global $wp_query;
  19. $cat_obj = $wp_query->get_queried_object();
  20. $thisCat = $cat_obj->term_id;
  21. $thisCat = get_category($thisCat);
  22. $parentCat = get_category($thisCat->parent);
  23. if ($thisCat->parent != 0) echo(get_category_parents($parentCat, TRUE, ' ' . $delimiter . ' '));
  24. echo $currentBefore . '';
  25. single_cat_title();
  26. echo '' . $currentAfter;
  27.  
  28. } elseif ( is_day() ) {
  29. echo '<a href="' . get_year_link(get_the_time('Y')) . '">' . get_the_time('Y') . '</a> ' . $delimiter . ' ';
  30. echo '<a href="' . get_month_link(get_the_time('Y'),get_the_time('m')) . '">' . get_the_time('F') . '</a> ' . $delimiter . ' ';
  31. echo $currentBefore . get_the_time('d') . $currentAfter;
  32.  
  33. } elseif ( is_month() ) {
  34. echo '<a href="' . get_year_link(get_the_time('Y')) . '">' . get_the_time('Y') . '</a> ' . $delimiter . ' ';
  35. echo $currentBefore . get_the_time('F') . $currentAfter;
  36.  
  37. } elseif ( is_year() ) {
  38. echo $currentBefore . get_the_time('Y') . $currentAfter;
  39.  
  40. } elseif ( is_single() ) {
  41. $cat = get_the_category(); $cat = $cat[0];
  42. echo get_category_parents($cat, TRUE, ' ' . $delimiter . ' ');
  43. echo $currentBefore;
  44. the_title();
  45. echo $currentAfter;
  46.  
  47. } elseif ( is_page() && !$post->post_parent ) {
  48. echo $currentBefore;
  49. the_title();
  50. echo $currentAfter;
  51.  
  52. } elseif ( is_page() && $post->post_parent ) {
  53. $parent_id = $post->post_parent;
  54. $breadcrumbs = array();
  55. while ($parent_id) {
  56. $page = get_page($parent_id);
  57. $breadcrumbs[] = '<a href="' . get_permalink($page->ID) . '">' . get_the_title($page->ID) . '</a>';
  58. $parent_id = $page->post_parent;
  59. }
  60. $breadcrumbs = array_reverse($breadcrumbs);
  61. foreach ($breadcrumbs as $crumb) echo $crumb . ' ' . $delimiter . ' ';
  62. echo $currentBefore;
  63. the_title();
  64. echo $currentAfter;
  65.  
  66. } elseif ( is_search() ) {
  67. echo $currentBefore . 'Search results for &#39;' . get_search_query() . '&#39;' . $currentAfter;
  68.  
  69. } elseif ( is_tag() ) {
  70. echo $currentBefore . 'Posts tagged &#39;';
  71. single_tag_title();
  72. echo '&#39;' . $currentAfter;
  73.  
  74. } elseif ( is_author() ) {
  75. global $author;
  76. $userdata = get_userdata($author);
  77. echo $currentBefore . 'Articles posted by ' . $userdata->display_name . $currentAfter;
  78.  
  79. } elseif ( is_404() ) {
  80. echo $currentBefore . 'Error 404' . $currentAfter;
  81. }
  82.  
  83. if ( get_query_var('paged') ) {
  84. if ( is_category() || is_day() || is_month() || is_year() || is_search() || is_tag() || is_author() ) echo ' (';
  85. echo __('Page') . ' ' . get_query_var('paged');
  86. if ( is_category() || is_day() || is_month() || is_year() || is_search() || is_tag() || is_author() ) echo ')';
  87. }
  88.  
  89. echo '</div>';
  90.  
  91. }
  92. }
  93.  
  94. //Initialize with
  95.  
  96. if (function_exists('dimox_breadcrumbs')) dimox_breadcrumbs();

Report this snippet


Comments

RSS Icon Subscribe to comments

You need to login to post a comment.