Echo all category names, apart from one

I’ve got this simple code, that creates breadcrumbs:

function the_breadcrumb() {
    if (!is_home()) {
        echo '<a href="';
        echo get_option('home');
        echo '">';
        echo 'home';
        echo " / ";
        if (is_category() || is_single()) {
            the_category(' / ');
            if (is_single()) {
                echo " / " . '<a href="#">' . get_the_title() . '</a>';
            }
        } elseif (is_page()) {
            echo " / " . '<a href="#">' . get_the_title()  . '</a>';
        }
    }
}

And this echoes out:

Read More
<p class="bread-crumbs">
    <a href="http://fashion-detail.dev">home / </a>
    <a href="http://fashion-detail.dev/?cat=4" title="View all posts in Fashion" rel="category">Fashion</a> / 
    <a href="http://fashion-detail.dev/?cat=16" title="View all posts in Top" rel="category">Top</a> / 
    <a href="#">This is in the fashion category.</a>        
</p>

However, it do not want it to show the a href tag that links to the top category. Is there a way to include a str_replace, or just prevent the_category from calling top (I still need the post, I just don’t want to link to top in the breadcrumbs)?

Related posts

1 comment

  1. the_category echos content. You will not be able to use string manipulation. No strings are returned that you could manipulate. They are just echoed immediately.

    You could completely filter out a particular category, which seems to be an option, with this:

    function cat_filter_wpse_137596($categories) {
      foreach ($categories as $k => $c) {
        if ('uncategorized' === $c->slug) {
          unset($categories[$k]);
        }
      }
      return $categories;
    }
    

    You’d use it like:

        add_filter('get_the_categories','cat_filter_wpse_137596');
        if (is_category() || is_single()) {
            the_category(' / ');
            if (is_single()) {
                echo " / " . '<a href="#">' . get_the_title() . '</a>';
            }
        } elseif (is_page()) {
            echo " / " . '<a href="#">' . get_the_title()  . '</a>';
        }
        remove_filter('get_the_categories','cat_filter_wpse_137596');
    

    There is no easy way to keep the category and remove the link. You’d need some nasty regex on the the_category filter.

Comments are closed.