Skip to content

Instantly share code, notes, and snippets.

@zofe
Last active December 17, 2015 11:49
Show Gist options
  • Save zofe/5605150 to your computer and use it in GitHub Desktop.
Save zofe/5605150 to your computer and use it in GitHub Desktop.
a simple !? breadcrumb for wordpress hugs
/**
* wordpress breadcrumb function
*
* @author Felice Ostuni <[email protected]>
* @param tring $domain for translations (usually your theme folder name)
*/
function rpd_breadcrumbs($domain=null) {
global $post;
$breadcrumbs[] = '<a href="' . home_url() . '">Home</a>';
if (is_category()){
$cat = get_category(get_query_var('cat'), false);
if ($cat->parent != 0) {
$breadcrumbs += array_filter(explode('|',trim(get_category_parents($cat->parent, TRUE,'|'))));
}
$breadcrumbs[] = single_cat_title('', false);
} elseif (is_search()){
$breadcrumbs[] = __('Results for ', $domain).' "' . get_search_query() . '"';
} elseif (is_day()){
$breadcrumbs[] = '<a href="' . get_year_link(get_the_time('Y')) . '">' . get_the_time('Y') . '</a>';
$breadcrumbs[] = '<a href="' . get_month_link(get_the_time('Y'),get_the_time('m')) . '">' . get_the_time('F') . '</a>';
$breadcrumbs[] = get_the_time('d');
} elseif (is_month()){
$breadcrumbs[] = '<a href="' . get_year_link(get_the_time('Y')) . '">' . get_the_time('Y') . '</a>';
$breadcrumbs[] = get_the_time('F');
} elseif (is_year()){
$breadcrumbs[] = get_the_time('Y');
} elseif (is_single() && !is_attachment()){
if (get_post_type() != 'post'){
$post_type = get_post_type_object(get_post_type());
$slug = $post_type->rewrite;
$breadcrumbs[] = '<a href="' . get_bloginfo('url') . '/' . $slug['slug'] . '/">' . $post_type->labels->singular_name . '</a>';
$breadcrumbs[] = get_the_title();
} else {
$cat = get_the_category();
$cat = $cat[0];
$breadcrumbs += array_filter(explode('|',trim(get_category_parents($cat, TRUE,'|'))));
$breadcrumbs[] = get_the_title();
}
} elseif(!is_single() && !is_page() && get_post_type() != 'post' && !is_404()){
$post_type = get_post_type_object(get_post_type());
$breadcrumbs[] = $post_type->labels->singular_name;
} elseif (is_attachment()){
$parent = get_post($post->post_parent);
$cat = get_the_category($parent->ID);
$cat = $cat[0];
$breadcrumbs += array_filter(explode('|',trim(get_category_parents($cat, TRUE,'|'))));
$breadcrumbs[] = get_the_title();
$breadcrumbs[] = '<a href="' . get_permalink($parent) . '">' . $parent->post_title . '</a>';
$breadcrumbs[] = get_the_title();
} elseif (is_page()){
if($post->post_parent) {
$parent_id = $post->post_parent;
while ($parent_id) {
$page = get_page($parent_id);
$reversed_breadcrumbs[] = '<a href="' . get_permalink($page->ID) . '">' . get_the_title($page->ID) . '</a>';
$parent_id = $page->post_parent;
}
if (count($reversed_breadcrumbs)){
$breadcrumbs += array_reverse($reversed_breadcrumbs);
}
}
$breadcrumbs[] = get_the_title($post->ID);
} elseif (is_tag()) {
$breadcrumbs[] = __('Results for tag ', $domain).' "' . single_tag_title('', false) . '"';
} elseif (is_author()){
global $author;
$userdata = get_userdata($author);
$breadcrumbs[] = __('Posted by ', $domain).' ' . $userdata->display_name;
} elseif (is_404()){
$breadcrumbs[] = __('Error 404', $domain);
}
$result = '';
$i = 0;
$end = count($breadcrumbs);
foreach($breadcrumbs as $crumb)
{
if ($i < $end - 1) {
$result .= '<li>'. $crumb.' <span class="divider">/</span><li>';
} else {
$result .= '<li class="active">'.$crumb.'</li>';
}
$i++;
}
echo '<ul class="breadcrumb">'.$result.'</ul>';
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment