Advertisement
kobial8

Breadcrumb Dynamic

Mar 15th, 2015
222
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
PHP 4.78 KB | None | 0 0
  1. Step 01: put the codes in functions.php file:
  2.  
  3. function qt_custom_breadcrumbs() {
  4.   $showOnHome = 0; // 1 - show breadcrumbs on the homepage, 0 - don't show
  5.   $delimiter = '»'; // delimiter between crumbs
  6.   $home = 'Home'; // text for the 'Home' link
  7.   $showCurrent = 1; // 1 - show current post/page title in breadcrumbs, 0 - don't show
  8.   $before = '<span class="current">'; // tag before the current crumb
  9.   $after = '</span>'; // tag after the current crumb
  10.   global $post;
  11.   $homeLink = get_bloginfo('url');
  12.   if (is_home() || is_front_page()) {
  13.     if ($showOnHome == 1) echo '<div id="crumbs"><a href="' . $homeLink . '">' . $home . '</a></div>';
  14.   } else {
  15.     echo '<div id="crumbs"><a href="' . $homeLink . '">' . $home . '</a> ' . $delimiter . ' ';
  16.     if ( is_category() ) {
  17.       $thisCat = get_category(get_query_var('cat'), false);
  18.       if ($thisCat->parent != 0) echo get_category_parents($thisCat->parent, TRUE, ' ' . $delimiter . ' ');
  19.       echo $before . 'Archive by category "' . single_cat_title('', false) . '"' . $after;
  20.     } elseif ( is_search() ) {
  21.       echo $before . 'Search results for "' . get_search_query() . '"' . $after;
  22.     } elseif ( is_day() ) {
  23.       echo '<a href="' . get_year_link(get_the_time('Y')) . '">' . get_the_time('Y') . '</a> ' . $delimiter . ' ';
  24.       echo '<a href="' . get_month_link(get_the_time('Y'),get_the_time('m')) . '">' . get_the_time('F') . '</a> ' . $delimiter . ' ';
  25.       echo $before . get_the_time('d') . $after;
  26.     } elseif ( is_month() ) {
  27.       echo '<a href="' . get_year_link(get_the_time('Y')) . '">' . get_the_time('Y') . '</a> ' . $delimiter . ' ';
  28.       echo $before . get_the_time('F') . $after;
  29.     } elseif ( is_year() ) {
  30.       echo $before . get_the_time('Y') . $after;
  31.     } elseif ( is_single() && !is_attachment() ) {
  32.       if ( get_post_type() != 'post' ) {
  33.         $post_type = get_post_type_object(get_post_type());
  34.         $slug = $post_type->rewrite;
  35.         echo '<a href="' . $homeLink . '/' . $slug['slug'] . '/">' . $post_type->labels->singular_name . '</a>';
  36.         if ($showCurrent == 1) echo ' ' . $delimiter . ' ' . $before . get_the_title() . $after;
  37.       } else {
  38.         $cat = get_the_category(); $cat = $cat[0];
  39.         $cats = get_category_parents($cat, TRUE, ' ' . $delimiter . ' ');
  40.         if ($showCurrent == 0) $cats = preg_replace("#^(.+)\s$delimiter\s$#", "$1", $cats);
  41.         echo $cats;
  42.         if ($showCurrent == 1) echo $before . get_the_title() . $after;
  43.       }
  44.     } elseif ( !is_single() && !is_page() && get_post_type() != 'post' && !is_404() ) {
  45.       $post_type = get_post_type_object(get_post_type());
  46.       echo $before . $post_type->labels->singular_name . $after;
  47.     } elseif ( is_attachment() ) {
  48.       $parent = get_post($post->post_parent);
  49.       $cat = get_the_category($parent->ID); $cat = $cat[0];
  50.       echo get_category_parents($cat, TRUE, ' ' . $delimiter . ' ');
  51.       echo '<a href="' . get_permalink($parent) . '">' . $parent->post_title . '</a>';
  52.       if ($showCurrent == 1) echo ' ' . $delimiter . ' ' . $before . get_the_title() . $after;
  53.     } elseif ( is_page() && !$post->post_parent ) {
  54.       if ($showCurrent == 1) echo $before . get_the_title() . $after;
  55.     } elseif ( is_page() && $post->post_parent ) {
  56.       $parent_id  = $post->post_parent;
  57.       $breadcrumbs = array();
  58.       while ($parent_id) {
  59.         $page = get_page($parent_id);
  60.         $breadcrumbs[] = '<a href="' . get_permalink($page->ID) . '">' . get_the_title($page->ID) . '</a>';
  61.         $parent_id  = $page->post_parent;
  62.       }
  63.       $breadcrumbs = array_reverse($breadcrumbs);
  64.       for ($i = 0; $i < count($breadcrumbs); $i++) {
  65.         echo $breadcrumbs[$i];
  66.         if ($i != count($breadcrumbs)-1) echo ' ' . $delimiter . ' ';
  67.       }
  68.       if ($showCurrent == 1) echo ' ' . $delimiter . ' ' . $before . get_the_title() . $after;
  69.     } elseif ( is_tag() ) {
  70.       echo $before . 'Posts tagged "' . single_tag_title('', false) . '"' . $after;
  71.     } elseif ( is_author() ) {
  72.        global $author;
  73.       $userdata = get_userdata($author);
  74.       echo $before . 'Articles posted by ' . $userdata->display_name . $after;
  75.     } elseif ( is_404() ) {
  76.       echo $before . 'Error 404' . $after;
  77.     }
  78.     if ( get_query_var('paged') ) {
  79.       if ( is_category() || is_day() || is_month() || is_year() || is_search() || is_tag() || is_author() ) echo ' (';
  80.       echo __('Page') . ' ' . get_query_var('paged');
  81.       if ( is_category() || is_day() || is_month() || is_year() || is_search() || is_tag() || is_author() ) echo ')';
  82.     }
  83.     echo '</div>';
  84.   }
  85. } // end qt_custom_breadcrumbs()
  86.  
  87. Step 02 : After adding the code into functions.php add the below code to where you want to use this function:
  88.  
  89. <?php if (function_exists('qt_custom_breadcrumbs')) qt_custom_breadcrumbs(); ?>
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement