Breadcrumbs trail is a useful and easy to understand secondary navigation tool for large websites, The breadcrumbs trail offers a visually way to your site visitor to keep track and know where they are.
The term breadcrumb trail comes from the story of Hansel and Gretel, who left a trail of breadcrumbs as they walked through the forest so they could trace their way back home.
We've created this function to use in our
WordPress Themes even with custom post types and taxonomies, This simple function will generates locational breadcrumb trails for your
WordPress powered blog or website.
You are free to use
Mine Breadcrumbs trail in your
WordPress themes for personal or commercial use, feedback and improvements are always welcome, simply copy this code below and paste it in your theme's
functions.php, have a question don't hesitate leave us a comment.
[code type=php]
'; ?>
You are here: Home';
// Breadcrumb for category and sub-category archive
if (is_category()) {
echo $separator .'- ';
$cat_obj = $wp_query->get_queried_object();
$thisCat = $cat_obj->term_id;
$thisCat = get_category($thisCat);
$parentCat = get_category($thisCat->parent);
if ($thisCat->parent != 0) echo get_category_parents($parentCat, true, '
' .$separator .'- ');
echo 'Archive by Category - ' .single_cat_title() .'
';
}
// breadcrumbs for custom taxonomy terms
elseif (is_tax()) {
// get post taxonomy
$taxonomy = get_query_var('taxonomy');
$object_id = get_queried_object_id();
$term = get_term($object_id, $taxonomy);
if (!empty ($term)) {
$term_name = $term->name;
// get term link
$term_link = get_term_link((int)$term->term_id, $taxonomy);
}
$term_parent = $term->parent;
// get all parents terms
while($term_parent) {
$term = get_term($term_parent, $taxonomy);
$term_parent = $term->parent;
$term_parents[] = array('name' => $term->name, 'url' => get_term_link((int)$term->term_id, $taxonomy));
}
if(!empty($term_parents)){
$term_parents = array_reverse($term_parents);
foreach($term_parents as $term){
$output .= $separator .'- ' .$term['name'] .'
';
echo $output;
}
}
echo $separator .'- ' .__('Archive for', THEME_NAME) .' ' .ucfirst(substr($taxonomy, 0, -1)) .' - ' .$term_name .'
';
}
// Breadcrumb for daily archives
elseif (is_day()) {
echo $separator .'- ' .get_the_time('Y') .'
';
echo $separator .'- ' .get_the_time('F') .'
';
echo $separator .'- ' .get_the_time('d') .'
';
}
// Breadcrumb for monthly archive
elseif (is_month()) {
echo $separator .'- ' .get_the_time('Y') .'
';
echo $separator .'- ' .get_the_time('F') .'
';
}
// Breadcrumb for yearly archive
elseif (is_year()) {
echo $separator .'- ' .get_the_time('Y') .'
';
}
// Breadcrumb for single post and attachments
elseif (is_single() && !is_attachment()) {
if (get_post_type() == $post_type ) {
// get post taxonomy
$taxonomies = get_object_taxonomies($post_type);
foreach ($taxonomies as $taxonomy) {
$taxonomy = $taxonomy;
}
$terms = get_the_terms( $post_id, $taxonomy );
if (!empty ($terms)) {
foreach ($terms as $term) {
$term_name = $term->name;
// get term link
$term_link = get_term_link((int)$term->term_id, $taxonomy);
// get term parent
$term_parent = $term->parent;
}
}
// get all parents terms
while($term_parent) {
$term = get_term($term_parent, $taxonomy);
$term_parent = $term->parent;
$term_parents[] = array('name' => $term->name, 'url' => get_term_link((int)$term->term_id, $taxonomy));
}
if (!empty($term_parents)) {
$term_parents = array_reverse($term_parents);
foreach($term_parents as $term) {
$output .= $separator .'- ' .$term['name'] .'
';
echo $output;
}
}
echo $separator .'- ' .$term_name .'
';
echo $separator .'- ' .get_the_title($post->post_parent) .'
';
} else {
echo $separator .'- ' .substr(get_category_parents($category[0]->cat_ID, true, '
' . $separator .'- '),0, -9) .'
';
echo '- ' .get_the_title($post->post_parent) .'
';
}
}
// Breadcrumb for posts page (Blog page)
elseif ((is_home()) && (!is_front_page())) {
echo $separator .'- ' .get_the_title( get_option('page_for_posts') ) .'
';
}
// Breadcrumb for pages
elseif ((is_page()) && (!is_front_page())) {
$ancestors = get_post_ancestors($post);
if ($ancestors) {
$ancestors = array_reverse($ancestors);
foreach ($ancestors as $ancestor) {
echo $separator .'- ' .get_the_title($ancestor) .'
';
}
}
echo $separator .'- ' .get_the_title($post) .'
';
}
// Breadcrumb for error 404 page
elseif (is_404()) {
echo $separator .'- - ' .__('404 - Page Not Found', THEME_NAME) .'
';
}
// Breadcrumb for search result page
elseif (is_search()) {
echo $separator .'- ' .__('Search Results for', THEME_NAME) .' - ' .wp_specialchars(get_query_var('s')) .'
';
}
// Breadcrumb for tag archives
elseif (is_tag()) {
echo $separator .'- ' .__('Archive for Tag', THEME_NAME) .' - ' .$tag->name .'
';
}
// Breadcrumb for author archive
elseif (is_author()) {
echo $separator .'- ' .__('Articles Posted by', THEME_NAME) .' - ' .$userdata->display_name .'
';
} ?>
[/code]
Add this code below in theme's template files where you want to show breadcrumbs navigation.
[code type=php]
[/code]