Advertisement
Guest User

breadcrumb

a guest
Sep 12th, 2016
70
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 5.54 KB | None | 0 0
  1. <?php
  2. /*
  3.  
  4. Source: http://dimox.net/wordpress-breadcrumbs-without-a-plugin/
  5.  
  6. */
  7.  
  8. if (!function_exists('breadcrumbs')) {
  9. function breadcrumbs() {
  10.  
  11. /* === OPTIONS === */
  12. $text['home'] = __('Home', 'medusa'); // text for the 'Home' link
  13. $text['category'] = __('Archive by Category "%s"', 'medusa'); // text for a category page
  14. $text['search'] = __('Search Results for "%s" query', 'medusa'); // text for a search results page
  15. $text['tag'] = __('Posts Tagged "%s"', 'medusa'); // text for a tag page
  16. $text['author'] = __('Articles Posted by %s', 'medusa'); // text for an author page
  17. $text['404'] = __('Error 404', 'medusa'); // text for the 404 page
  18.  
  19. $showCurrent = 1; // 1 - show current post/page title in breadcrumbs, 0 - don't show
  20. $showOnHome = 1; // 1 - show breadcrumbs on the homepage, 0 - don't show
  21. $delimiter = '&raquo'; // delimiter between crumbs
  22. $before = '<span class="current">'; // tag before the current crumb
  23. $after = '</span>'; // tag after the current crumb
  24. /* === END OF OPTIONS === */
  25.  
  26. global $post;
  27. $homeLink = home_url();
  28. $linkBefore = '<span>';
  29. $linkAfter = '</span>';
  30. $linkAttr = '';
  31. $link = $linkBefore . '<a' . $linkAttr . ' href="%1$s">%2$s</a>' . $linkAfter;
  32.  
  33.  
  34. echo '<div id="crumbs" class="tw-breadcrumb pull-right">';
  35.  
  36. if (is_home() || is_front_page()) {
  37.  
  38. if ($showOnHome == 1) echo '<span><a href="' . $homeLink . '">' . $text['home'] . '</a></span>';
  39.  
  40. } else {
  41.  
  42. echo sprintf($link, $homeLink, $text['home']) . $delimiter;
  43.  
  44. if ( is_category() ) {
  45. $thisCat = get_category(get_query_var('cat'), false);
  46. if ($thisCat->parent != 0) {
  47. $cats = get_category_parents($thisCat->parent, TRUE, $delimiter);
  48. $cats = str_replace('<a', $linkBefore . '<a' . $linkAttr, $cats);
  49. $cats = str_replace('</a>', '</a>' . $linkAfter, $cats);
  50. echo $cats;
  51. }
  52. echo $before . sprintf($text['category'], single_cat_title('', false)) . $after;
  53.  
  54. } elseif ( is_search() ) {
  55. echo $before . sprintf($text['search'], get_search_query()) . $after;
  56.  
  57. } elseif ( is_day() ) {
  58. echo sprintf($link, get_year_link(get_the_time('Y')), get_the_time('Y')) . $delimiter;
  59. echo sprintf($link, get_month_link(get_the_time('Y'),get_the_time('m')), get_the_time('F')) . $delimiter;
  60. echo $before . get_the_time('d') . $after;
  61.  
  62. } elseif ( is_month() ) {
  63. echo sprintf($link, get_year_link(get_the_time('Y')), get_the_time('Y')) . $delimiter;
  64. echo $before . get_the_time('F') . $after;
  65.  
  66. } elseif ( is_year() ) {
  67. echo $before . get_the_time('Y') . $after;
  68.  
  69. } elseif ( is_single() && !is_attachment() ) {
  70. if ( get_post_type() != 'post' ) {
  71. $post_type = get_post_type_object(get_post_type());
  72. $slug = $post_type->rewrite;
  73. printf($link, $homeLink . $slug['slug'] . '/', $post_type->labels->singular_name);
  74. if ($showCurrent == 1) echo $delimiter . $before . get_the_title() . $after;
  75. } else {
  76. $cat = get_the_category(); $cat = $cat[0];
  77. $cats = get_category_parents($cat, TRUE, $delimiter);
  78. if ($showCurrent == 0) $cats = preg_replace("#^(.+)$delimiter$#", "$1", $cats);
  79. $cats = str_replace('<a', $linkBefore . '<a' . $linkAttr, $cats);
  80. $cats = str_replace('</a>', '</a>' . $linkAfter, $cats);
  81. echo $cats;
  82. if ($showCurrent == 1) echo $before . get_the_title() . $after;
  83. }
  84.  
  85. } elseif ( !is_single() && !is_page() && get_post_type() != 'post' && !is_404() ) {
  86. $post_type = get_post_type_object(get_post_type());
  87. if($post_type){
  88. echo $before . $post_type->labels->singular_name . $after;
  89. }
  90. } elseif ( is_attachment() ) {
  91. $parent = get_post($post->post_parent);
  92. $cat = get_the_category($parent->ID); $cat = $cat[0];
  93. $cats = get_category_parents($cat, TRUE, $delimiter);
  94. $cats = str_replace('<a', $linkBefore . '<a' . $linkAttr, $cats);
  95. $cats = str_replace('</a>', '</a>' . $linkAfter, $cats);
  96. echo $cats;
  97. printf($link, get_permalink($parent), $parent->post_title);
  98. if ($showCurrent == 1) echo $delimiter . $before . get_the_title() . $after;
  99.  
  100. } elseif ( is_page() && !$post->post_parent ) {
  101. if ($showCurrent == 1) echo $before . get_the_title() . $after;
  102.  
  103. } elseif ( is_page() && $post->post_parent ) {
  104. $parent_id = $post->post_parent;
  105. $breadcrumbs = array();
  106. while ($parent_id) {
  107. $page = get_page($parent_id);
  108. $breadcrumbs[] = sprintf($link, get_permalink($page->ID), get_the_title($page->ID));
  109. $parent_id = $page->post_parent;
  110. }
  111. $breadcrumbs = array_reverse($breadcrumbs);
  112. for ($i = 0; $i < count($breadcrumbs); $i++) {
  113. echo $breadcrumbs[$i];
  114. if ($i != count($breadcrumbs)-1) echo $delimiter;
  115. }
  116. if ($showCurrent == 1) echo $delimiter . $before . get_the_title() . $after;
  117.  
  118. } elseif ( is_tag() ) {
  119. echo $before . sprintf($text['tag'], single_tag_title('', false)) . $after;
  120.  
  121. } elseif ( is_author() ) {
  122. global $author;
  123. $userdata = get_userdata($author);
  124. echo $before . sprintf($text['author'], $userdata->display_name) . $after;
  125.  
  126. } elseif ( is_404() ) {
  127. echo $before . $text['404'] . $after;
  128. }
  129.  
  130. if ( get_query_var('paged') ) {
  131. if ( is_category() || is_day() || is_month() || is_year() || is_search() || is_tag() || is_author() ) echo ' (';
  132. echo __('Page', 'medusa') . ' ' . get_query_var('paged');
  133. if ( is_category() || is_day() || is_month() || is_year() || is_search() || is_tag() || is_author() ) echo ')';
  134. }
  135.  
  136. }
  137. echo '</div>';
  138. } // end breadcrumbs()
  139. } ?>
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement