Advertisement
Guest User

breadcrumb

a guest
Oct 9th, 2016
103
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 6.24 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'] = tw_option('br_home') ? tw_option('br_home') : __('Home', 'flatco'); // text for the 'Home' link
  13. $text['category'] = tw_option('br_category') ? tw_option('br_category').' %s' : __('Archive by Category "%s"', 'flatco'); // text for a category page
  14. $text['search'] = tw_option('br_search') ? tw_option('br_search').' %s' : __('Search Results for "%s" query', 'flatco'); // text for a search results page
  15. $text['tag'] = tw_option('br_tag') ? tw_option('br_tag').' %s' : __('Posts Tagged "%s"', 'flatco'); // text for a tag page
  16. $text['author'] = tw_option('br_author') ? tw_option('br_author').' %s' : __('Articles Posted by %s', 'flatco'); // text for an author page
  17. $text['404'] = tw_option('br_404') ? tw_option('br_404') : __('Error 404', 'flatco'); // 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 = ''; // delimiter between crumbs
  22. $before = '<span class="crumb-item 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 class="crumb-item">';
  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. if(get_post_type() == 'tw_portfolio' && tw_option('port_page')){
  72. $page = get_page_by_path(tw_option('port_page'));
  73. if($page)
  74. printf($link, get_permalink($page), get_the_title($page));
  75. }
  76. if ($showCurrent == 1) echo $delimiter . $before . get_the_title() . $after;
  77. } else {
  78. $cat = get_the_category(); $cat = $cat[0];
  79. $cats = get_category_parents($cat, TRUE, $delimiter);
  80. if ($showCurrent == 0) $cats = preg_replace("#^(.+)$delimiter$#", "$1", $cats);
  81. $cats = str_replace('<a', $linkBefore . '<a' . $linkAttr, $cats);
  82. $cats = str_replace('</a>', '</a>' . $linkAfter, $cats);
  83. echo $cats;
  84. if ($showCurrent == 1) echo $before . get_the_title() . $after;
  85. }
  86.  
  87. } elseif ( !is_single() && !is_page() && get_post_type() != 'post' && !is_404() ) {
  88. $post_type = get_post_type_object(get_post_type());
  89. if($post_type){
  90. echo $before . $post_type->labels->singular_name . $after;
  91. }
  92. } elseif ( is_attachment() ) {
  93. $parent = get_post($post->post_parent);
  94. $cat = get_the_category($parent->ID);
  95. if($cat){
  96. $cat = $cat[0];
  97. $cats = get_category_parents($cat, TRUE, $delimiter);
  98. $cats = str_replace('<a', $linkBefore . '<a' . $linkAttr, $cats);
  99. $cats = str_replace('</a>', '</a>' . $linkAfter, $cats);
  100. echo $cats;
  101. }
  102. printf($link, get_permalink($parent), $parent->post_title);
  103. if ($showCurrent == 1) echo $delimiter . $before . get_the_title() . $after;
  104.  
  105. } elseif ( is_page() && !$post->post_parent ) {
  106. if ($showCurrent == 1) echo $before . get_the_title() . $after;
  107.  
  108. } elseif ( is_page() && $post->post_parent ) {
  109. $parent_id = $post->post_parent;
  110. $breadcrumbs = array();
  111. while ($parent_id) {
  112. $page = get_page($parent_id);
  113. $breadcrumbs[] = sprintf($link, get_permalink($page->ID), get_the_title($page->ID));
  114. $parent_id = $page->post_parent;
  115. }
  116. $breadcrumbs = array_reverse($breadcrumbs);
  117. for ($i = 0; $i < count($breadcrumbs); $i++) {
  118. echo $breadcrumbs[$i];
  119. if ($i != count($breadcrumbs)-1) echo $delimiter;
  120. }
  121. if ($showCurrent == 1) echo $delimiter . $before . get_the_title() . $after;
  122.  
  123. } elseif ( is_tag() ) {
  124. echo $before . sprintf($text['tag'], single_tag_title('', false)) . $after;
  125.  
  126. } elseif ( is_author() ) {
  127. global $author;
  128. $userdata = get_userdata($author);
  129. echo $before . sprintf($text['author'], $userdata->display_name) . $after;
  130.  
  131. } elseif ( is_404() ) {
  132. echo $before . $text['404'] . $after;
  133. }
  134.  
  135. if ( get_query_var('paged') ) {
  136. if ( is_category() || is_day() || is_month() || is_year() || is_search() || is_tag() || is_author() ) echo ' (';
  137. echo __('Page', 'flatco') . ' ' . get_query_var('paged');
  138. if ( is_category() || is_day() || is_month() || is_year() || is_search() || is_tag() || is_author() ) echo ')';
  139. }
  140.  
  141. }
  142. echo '</div>';
  143. } // end breadcrumbs()
  144. } ?>
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement