Creating breadcrumbs in WordPress themes can be a pain. With that in mind, this handy function should be exactly what you need.
function waz_breadcrumbs()
{
$delimiter = '»';
$name = 'Home'; //text for the 'Home' link
$currentBefore = '<span>';
$currentAfter = '</span>';
if ( !is_home() && !is_front_page() || is_paged() ) {
<a href="http://www.php.net/echo">echo</a> '<div id="crumbs">';
<a href="http://www.php.net/global">global</a> $post;
$home = get_bloginfo('url');
<a href="http://www.php.net/echo">echo</a> '<a href="' . $home . '">' . $name . '</a> ' . $delimiter . ' ';
if ( is_category() ) {
<a href="http://www.php.net/global">global</a> $wp_query;
$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) <a href="http://www.php.net/echo">echo</a>(get_category_parents($parentCat, TRUE, ' ' . $delimiter . ' '));
<a href="http://www.php.net/echo">echo</a> $currentBefore . '';
single_cat_title();
<a href="http://www.php.net/echo">echo</a> '' . $currentAfter;
} elseif ( is_day() ) {
<a href="http://www.php.net/echo">echo</a> '<a href="' . get_year_link(get_the_time('Y')) . '">' . get_the_time('Y') . '</a> ' . $delimiter . ' ';
<a href="http://www.php.net/echo">echo</a> '<a href="' . get_month_link(get_the_time('Y'),get_the_time('m')) . '">' . get_the_time('F') . '</a> ' . $delimiter . ' ';
<a href="http://www.php.net/echo">echo</a> $currentBefore . get_the_time('d') . $currentAfter;
} elseif ( is_month() ) {
<a href="http://www.php.net/echo">echo</a> '<a href="' . get_year_link(get_the_time('Y')) . '">' . get_the_time('Y') . '</a> ' . $delimiter . ' ';
<a href="http://www.php.net/echo">echo</a> $currentBefore . get_the_time('F') . $currentAfter;
} elseif ( is_year() ) {
<a href="http://www.php.net/echo">echo</a> $currentBefore . get_the_time('Y') . $currentAfter;
} elseif ( is_single() ) {
$cat = get_the_category(); $cat = $cat[fusion_builder_container hundred_percent="yes" overflow="visible"][fusion_builder_row][fusion_builder_column type="1_1" background_position="left top" background_color="" border_size="" border_color="" border_style="solid" spacing="yes" background_image="" background_repeat="no-repeat" padding="" margin_top="0px" margin_bottom="0px" class="" id="" animation_type="" animation_speed="0.3" animation_direction="left" hide_on_mobile="no" center_content="no" min_height="none"][0];
<a href="http://www.php.net/echo">echo</a> get_category_parents($cat, TRUE, ' ' . $delimiter . ' ');
<a href="http://www.php.net/echo">echo</a> $currentBefore;
the_title();
<a href="http://www.php.net/echo">echo</a> $currentAfter;
} elseif ( is_page() && !$post->post_parent ) {
<a href="http://www.php.net/echo">echo</a> $currentBefore;
the_title();
<a href="http://www.php.net/echo">echo</a> $currentAfter;
} elseif ( is_page() && $post->post_parent ) {
$parent_id = $post->post_parent;
$breadcrumbs = <a href="http://www.php.net/array">array</a>();
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 = <a href="http://www.php.net/array_reverse">array_reverse</a>($breadcrumbs);
foreach ($breadcrumbs as $crumb) <a href="http://www.php.net/echo">echo</a> $crumb . ' ' . $delimiter . ' ';
<a href="http://www.php.net/echo">echo</a> $currentBefore;
the_title();
<a href="http://www.php.net/echo">echo</a> $currentAfter;
} elseif ( is_search() ) {
<a href="http://www.php.net/echo">echo</a> $currentBefore . 'Search results for &#39;' . get_search_query() . '&#39;' . $currentAfter;
} elseif ( is_tag() ) {
<a href="http://www.php.net/echo">echo</a> $currentBefore . 'Posts tagged &#39;';
single_tag_title();
<a href="http://www.php.net/echo">echo</a> '&#39;' . $currentAfter;
} elseif ( is_author() ) {
<a href="http://www.php.net/global">global</a> $author;
$userdata = get_userdata($author);
<a href="http://www.php.net/echo">echo</a> $currentBefore . 'Articles posted by ' . $userdata->display_name . $currentAfter;
} elseif ( is_404() ) {
<a href="http://www.php.net/echo">echo</a> $currentBefore . 'Error 404' . $currentAfter;
}
if ( get_query_var('paged') ) {
if ( is_category() || is_day() || is_month() || is_year() || is_search() || is_tag() || is_author() ) <a href="http://www.php.net/echo">echo</a> ' (';
<a href="http://www.php.net/echo">echo</a> __('Page') . ' ' . get_query_var('paged');
if ( is_category() || is_day() || is_month() || is_year() || is_search() || is_tag() || is_author() ) <a href="http://www.php.net/echo">echo</a> ')';
}
<a href="http://www.php.net/echo">echo</a> '</div>';
}
}
[/fusion_builder_column][/fusion_builder_row][/fusion_builder_container]
Leave A Comment