/ Published in: PHP
A custom breadcrumb function for WordPress that I created for a client.
You can pass 2 arguments, the first being the text you want to display in front of the breadcrumb and the second is to hide the home item in the breadcrumb
It will give each item a class and also the last item will receive a class active so you can style it more easily. I always welcome feedback (good or bad)
You can pass 2 arguments, the first being the text you want to display in front of the breadcrumb and the second is to hide the home item in the breadcrumb
It will give each item a class and also the last item will receive a class active so you can style it more easily. I always welcome feedback (good or bad)
Expand |
Embed | Plain Text
Copy this code and paste it in your HTML
function the_breadcrumb($before_text = 'You are here:', $home = false) { if(is_home() && $home != false){ $breads['home'] = 'Home'; } else { if (get_post_type() && get_post_type() != 'page' && get_post_type() != 'post' ) { $breads['post_type'] = get_post_type(); } elseif (get_post_type() == 'post') { $breads['blog'] = 'Blog'; } elseif (get_post_type() == 'page') { $breads['page'] = get_the_title(); } if (is_category() || is_single()) { foreach((get_the_category()) as $category) { $breads['category'][] = $category->cat_name; } if (is_single()) { $breads['single'] = get_the_title(); } } elseif (is_tag()) { $breads['tags'] = single_tag_title();} elseif (is_day()) { $breads['archive'] = "Archive for "; the_time('F jS, Y');} elseif (is_month()) { $breads['archive'] = "Archive for "; the_time('F, Y');} elseif (is_year()) { $breads['archive'] = "Archive for "; the_time('Y');} elseif (is_author()) {$breads['author_archive'] = "Author Archive";} elseif (is_search()) {$breads['search'] = "Search Results";} } echo $before_text . ' '; $i = 0; $b = ''; foreach ($breads as $key => $bread) { $i++; if ($i == $breadItems) { $active = 'active'; } $b .= '<span class="bread_item bread_item_' . $key . ' ' . $active . '">'; if ($key == 'home') { $b .= '<a href="' . get_option('home') . '">' . $bread . '</a>'; } elseif($key == 'category') { foreach ($breads['category'] as $cat) { $cats = $cat . '/ '; } } else { $b .= $bread; } $b .= '</span> » '; } }