Advertisement
timbrand

Wordpress Full Breadcrumbs

Sep 19th, 2013
877
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
PHP 6.25 KB | None | 0 0
  1. <?php
  2. function theme_breadcrumbs() {
  3.  
  4.     /* === OPTIONS === */
  5.     $text['home']     = 'Home'; // text for the 'Home' link
  6.     $text['category'] = 'Archive by Category "%s"'; // text for a category page
  7.     $text['search']   = 'Search Results for "%s" Query'; // text for a search results page
  8.     $text['tag']      = 'Posts Tagged "%s"'; // text for a tag page
  9.     $text['author']   = 'Articles Posted by %s'; // text for an author page
  10.     $text['404']      = 'Error 404'; // text for the 404 page
  11.  
  12.     $show_current   = 1; // 1 - show current post/page/category title in breadcrumbs, 0 - don't show
  13.     $show_on_home   = 0; // 1 - show breadcrumbs on the homepage, 0 - don't show
  14.     $show_home_link = 1; // 1 - show the 'Home' link, 0 - don't show
  15.     $show_title     = 1; // 1 - show the title for the links, 0 - don't show
  16.     $delimiter      = ' &raquo; '; // delimiter between crumbs
  17.     $before         = '<span class="current">'; // tag before the current crumb
  18.     $after          = '</span>'; // tag after the current crumb
  19.     /* === END OF OPTIONS === */
  20.  
  21.     global $post;
  22.     $home_link    = home_url('/');
  23.     $link_before  = '<span typeof="v:Breadcrumb">';
  24.     $link_after   = '</span>';
  25.     $link_attr    = ' rel="v:url" property="v:title"';
  26.     $link         = $link_before . '<a' . $link_attr . ' href="%1$s">%2$s</a>' . $link_after;
  27.     $parent_id    = $parent_id_2 = $post->post_parent;
  28.     $frontpage_id = get_option('page_on_front');
  29.  
  30.     if (is_home() || is_front_page()) {
  31.  
  32.         if ($show_on_home == 1) echo '<div class="breadcrumbs"><a href="' . $home_link . '">' . $text['home'] . '</a></div>';
  33.  
  34.     } else {
  35.  
  36.         echo '<div class="breadcrumbs" xmlns:v="http://rdf.data-vocabulary.org/#">';
  37.         if ($show_home_link == 1) {
  38.             echo sprintf($link, $home_link, $text['home']);
  39.             if ($frontpage_id == 0 || $parent_id != $frontpage_id) echo $delimiter;
  40.         }
  41.  
  42.         if ( is_category() ) {
  43.             $this_cat = get_category(get_query_var('cat'), false);
  44.             if ($this_cat->parent != 0) {
  45.                 $cats = get_category_parents($this_cat->parent, TRUE, $delimiter);
  46.                 if ($show_current == 0) $cats = preg_replace("#^(.+)$delimiter$#", "$1", $cats);
  47.                 $cats = str_replace('<a', $link_before . '<a' . $link_attr, $cats);
  48.                 $cats = str_replace('</a>', '</a>' . $link_after, $cats);
  49.                 if ($show_title == 0) $cats = preg_replace('/ title="(.*?)"/', '', $cats);
  50.                 echo $cats;
  51.             }
  52.             if ($show_current == 1) 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, $home_link . '/' . $slug['slug'] . '/', $post_type->labels->singular_name);
  74.                 if ($show_current == 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 ($show_current == 0) $cats = preg_replace("#^(.+)$delimiter$#", "$1", $cats);
  79.                 $cats = str_replace('<a', $link_before . '<a' . $link_attr, $cats);
  80.                 $cats = str_replace('</a>', '</a>' . $link_after, $cats);
  81.                 if ($show_title == 0) $cats = preg_replace('/ title="(.*?)"/', '', $cats);
  82.                 echo $cats;
  83.                 if ($show_current == 1) echo $before . get_the_title() . $after;
  84.             }
  85.  
  86.         } elseif ( !is_single() && !is_page() && get_post_type() != 'post' && !is_404() ) {
  87.             $post_type = get_post_type_object(get_post_type());
  88.             echo $before . $post_type->labels->singular_name . $after;
  89.  
  90.         } elseif ( is_attachment() ) {
  91.             $parent = get_post($parent_id);
  92.             $cat = get_the_category($parent->ID); $cat = $cat[0];
  93.             $cats = is_wp_error( $cat_parents = get_category_parents($cat, TRUE, '' . $delimiter . '') ) ? '' : $cat_parents;
  94.             $cats = str_replace('<a', $link_before . '<a' . $link_attr, $cats);
  95.             $cats = str_replace('</a>', '</a>' . $link_after, $cats);
  96.             if ($show_title == 0) $cats = preg_replace('/ title="(.*?)"/', '', $cats);
  97.             echo $cats;
  98.             printf($link, get_permalink($parent), $parent->post_title);
  99.             if ($show_current == 1) echo $delimiter . $before . get_the_title() . $after;
  100.  
  101.         } elseif ( is_page() && !$parent_id ) {
  102.             if ($show_current == 1) echo $before . get_the_title() . $after;
  103.  
  104.         } elseif ( is_page() && $parent_id ) {
  105.             if ($parent_id != $frontpage_id) {
  106.                 $breadcrumbs = array();
  107.                 while ($parent_id) {
  108.                     $page = get_page($parent_id);
  109.                     if ($parent_id != $frontpage_id) {
  110.                         $breadcrumbs[] = sprintf($link, get_permalink($page->ID), get_the_title($page->ID));
  111.                     }
  112.                     $parent_id = $page->post_parent;
  113.                 }
  114.                 $breadcrumbs = array_reverse($breadcrumbs);
  115.                 for ($i = 0; $i < count($breadcrumbs); $i++) {
  116.                     echo $breadcrumbs[$i];
  117.                     if ($i != count($breadcrumbs)-1) echo $delimiter;
  118.                 }
  119.             }
  120.             if ($show_current == 1) {
  121.                 if ($show_home_link == 1 || ($parent_id_2 != 0 && $parent_id_2 != $frontpage_id)) echo $delimiter;
  122.                 echo $before . get_the_title() . $after;
  123.             }
  124.  
  125.         } elseif ( is_tag() ) {
  126.             echo $before . sprintf($text['tag'], single_tag_title('', false)) . $after;
  127.  
  128.         } elseif ( is_author() ) {
  129.             global $author;
  130.             $userdata = get_userdata($author);
  131.             echo $before . sprintf($text['author'], $userdata->display_name) . $after;
  132.  
  133.         } elseif ( is_404() ) {
  134.             echo $before . $text['404'] . $after;
  135.         }
  136.  
  137.         if ( get_query_var('paged') ) {
  138.             if ( is_category() || is_day() || is_month() || is_year() || is_search() || is_tag() || is_author() ) echo ' (';
  139.             echo __('Page') . ' ' . get_query_var('paged');
  140.             if ( is_category() || is_day() || is_month() || is_year() || is_search() || is_tag() || is_author() ) echo ')';
  141.         }
  142.  
  143.         echo '</div><!-- .breadcrumbs -->';
  144.  
  145.     }
  146. }
  147. ?>
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement