REVAMPED DIMOX BREADCRUMBS
the description
I've been using a particular breadcrumb building function on most of my WordPress sites lately. The original function, created by Dimox, is a great alternative to all of the confusing and limiting breadcrumb plugins out there, and is much easier to customize. But it too has its limitations, so this forked version aims to eliminate those.
My biggest issue with the original is that the entire breadcrumb path has to be one long string where delimiters are inline characters separating each crumb. Occasionally I like each crumb to be in its own block-level element (specifically an <li>
tag) so that I can use borders as the delimiters. My own blog is a good example of this.
So along with a few other improvements (primarily eliminating a handful of rogue trailing delimiters after the last crumb) I've modified the original function to handle both inline delimiters and unordered lists. The list version is created by changing the delimiter text to "li" instead of "»" (for example).
the code
To implement the breadcrumbs, you just add the following PHP to your WordPress theme's functions.php file, then call <?php revamped_dimox_breadcrumbs(); ?>
wherever you'd like it to appear.
<?php function revamped_dimox_breadcrumbs() { $css_class = ''; // just in case the id="crumbs" isn't enough // and you'd also like a class on the wrapper $show_on_home = 0; // 1 - show breadcrumbs on the homepage, 0 - don't show $delimiter = "li"; // delimiter between crumbs // if delimiter is set to "li" then crumbs becomes an unordered list. $home = 'Home'; // text for the 'Home' link $show_current = 1; // 1 - show current post/page title in breadcrumbs, 0 - don't show $before = '<span class="current">'; // tag before the current crumb $after = '</span>'; // tag after the current crumb if($delimiter == "li") { $crumb_tag = "ul"; $crumb_list = TRUE; } else { $crumb_tag = "div"; $crumb_list = FALSE; } global $post; $homeLink = get_bloginfo('url'); if (is_home() || is_front_page()) { if ($show_on_home == 1) { echo '<' . $crumb_tag . ' id="crumbs" class="' . $css_class . '">'; if($crumb_list) echo '<li>'; echo '<a href="' . $homeLink . '">' . $home . '</a>'; if($crumb_list) echo '</li>'; echo '</' . $crumb_tag . '>'; } } else { echo '<' . $crumb_tag . ' id="crumbs" class="' . $css_class . '">'; if($crumb_list) echo '<li>'; echo '<a href="' . $homeLink . '">' . $home . '</a>'; echo ($crumb_list) ? '</li>' : ' ' . $delimiter . ' '; if ( is_category() ) { global $wp_query; $cat_obj = $wp_query->get_queried_object(); $this_cat = $cat_obj->term_id; $this_cat = get_category($this_cat); $parentCat = get_category($this_cat->parent); if ($this_cat->parent != 0) { if($crumb_list) echo '<li>'; echo(get_category_parents($parentCat, TRUE, ($crumb_list) ? '</li><li>' : ' ' . $delimiter . ' ')); if($crumb_list) echo '</li>'; } if($crumb_list) echo '<li>'; echo $before . 'Archive by category "' . single_cat_title('', false) . '"' . $after; if($crumb_list) echo '</li>'; } elseif ( is_search() ) { if($crumb_list) echo '<li>'; echo $before . 'Search results for "' . get_search_query() . '"' . $after; if($crumb_list) echo '</li>'; } elseif ( is_day() ) { if($crumb_list) echo '<li>'; echo '<a href="' . get_year_link(get_the_time('Y')) . '">' . get_the_time('Y') . '</a>'; echo ($crumb_list) ? '</li><li>' : ' ' . $delimiter . ' '; echo '<a href="' . get_month_link(get_the_time('Y'),get_the_time('m')) . '">' . get_the_time('F') . '</a>'; echo ($crumb_list) ? '</li><li>' : ' ' . $delimiter . ' '; echo $before . get_the_time('d') . $after; if($crumb_list) echo '</li>'; } elseif ( is_month() ) { if($crumb_list) echo '<li>'; echo '<a href="' . get_year_link(get_the_time('Y')) . '">' . get_the_time('Y') . '</a>'; echo ($crumb_list) ? '</li><li>' : ' ' . $delimiter . ' '; echo $before . get_the_time('F') . $after; if($crumb_list) echo '</li>'; } elseif ( is_year() ) { if($crumb_list) echo '<li>'; echo $before . get_the_time('Y') . $after; if($crumb_list) echo '</li>'; } elseif ( is_single() && !is_attachment() ) { if ( get_post_type() != 'post' ) { $post_type = get_post_type_object(get_post_type()); $slug = $post_type->rewrite; if($crumb_list) echo '<li>'; echo '<a href="' . $homeLink . '/' . $slug['slug'] . '/">' . $post_type->labels->singular_name . '</a>'; if($crumb_list) echo '<li>'; if ($show_current == 1) { echo ($crumb_list) ? '<li>' : ' ' . $delimiter . ' '; echo $before . get_the_title() . $after; if($crumb_list) echo '</li>'; } } else { $cat = get_the_category(); $cat = $cat[0]; $cats = get_category_parents($cat, TRUE, ($crumb_list) ? '</li><li>' : ' ' . $delimiter . ' '); if ($show_current == 0) { if($crumb_list) $cats = substr($cats, 0, -4); else $cats = substr($cats, 0, -2-(strlen($delimiter)) ); } if($crumb_list) echo '<li>'; echo $cats; if ($show_current == 1) { echo $before . get_the_title() . $after; if($crumb_list) echo '</li>'; } } } elseif ( !is_single() && !is_page() && get_post_type() != 'post' && !is_404() ) { $post_type = get_post_type_object(get_post_type()); if($crumb_list) echo '<li>'; echo $before . $post_type->labels->singular_name . $after; if($crumb_list) echo '</li>'; } elseif ( is_attachment() ) { $parent = get_post($post->post_parent); $cat = get_the_category($parent->ID); $cat = $cat[0]; echo get_category_parents($cat, TRUE, ($crumb_list) ? '</li><li>' : ' ' . $delimiter . ' '); echo '<a href="' . get_permalink($parent) . '">' . $parent->post_title . '</a>'; if($crumb_list) echo '</li>'; if ($show_current == 1) { if($crumb_list) echo '<li>'; echo $before . get_the_title() . $after; if($crumb_list) echo '</li>'; } } elseif ( is_page() && !$post->post_parent ) { if ($show_current == 1) { if($crumb_list) echo '<li>'; echo $before . get_the_title() . $after; if($crumb_list) echo '</li>'; } } elseif ( is_page() && $post->post_parent ) { $parent_id = $post->post_parent; $breadcrumbs = array(); while ($parent_id) { $page = get_page($parent_id); $breadcrumbs[] = '<a href="' . get_permalink($page->ID) . '">' . get_the_title($page->ID) . '</a>'; $parent_id = $page->post_parent; } $breadcrumbs = array_reverse($breadcrumbs); $counter = 0; $breadcrumb_count = count($breadcrumbs); foreach ($breadcrumbs as $crumb) { $counter++; if($crumb_list) echo '<li>'; echo $crumb; if($crumb_list) { echo '</li>'; } else { if($show_current == 1 || $counter!=$breadcrumb_count) { echo ' ' . $delimiter . ' '; } } } if ($show_current == 1) { if($crumb_list) echo '<li>'; echo $before . get_the_title() . $after; if($crumb_list) echo '</li>'; } } elseif ( is_tag() ) { if($crumb_list) echo '<li>'; echo $before . 'Posts tagged "' . single_tag_title('', false) . '"' . $after; if($crumb_list) echo '</li>'; } elseif ( is_author() ) { global $author; $userdata = get_userdata($author); if($crumb_list) echo '<li>'; echo $before . 'Articles posted by ' . $userdata->display_name . $after; if($crumb_list) echo '</li>'; } elseif ( is_404() ) { if($crumb_list) echo '<li>'; echo $before . 'Error 404' . $after; if($crumb_list) echo '</li>'; } if ( get_query_var('paged') ) { if($crumb_list) echo '<li>'; elseif ( is_category() || is_day() || is_month() || is_year() || is_search() || is_tag() || is_author() ) echo ' ('; echo __('Page') . ' ' . get_query_var('paged'); if($crumb_list) echo '</li>'; elseif ( is_category() || is_day() || is_month() || is_year() || is_search() || is_tag() || is_author() ) echo ')'; } echo '</' . $crumb_tag . '>'; } } ?>