Return to Snippet

Revision: 53102
at November 13, 2011 21:10 by krike


Initial Code
function the_breadcrumb($before_text = 'You are here:', $home = false) {
    $breads = array();
    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 (isset($_GET['paged']) && !empty($_GET['paged'])) {$breads['blog_archive'] = "Blog Archives";}
        elseif (is_search()) {$breads['search'] = "Search Results";}
    }

    echo $before_text . ' ';
    $breadItems = count($breads);
    $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 . '/ ';
                }
                $b .= rtrim($cats, '/ ');
            } else {
                $b .= $bread;
            }
        $b .= '</span> &raquo; ';
    }
    echo rtrim($b, ' &raquo; ');
}

Initial URL

                                

Initial Description
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)

Initial Title
wordpress breadcrumb

Initial Tags
wordpress, function

Initial Language
PHP