Displaying category in breadcrumbs

Hello,

I tried all three code variations described here: https://theme.co/apex/forum/t/breadcrumbs-adding-category-and-blog-page-home-plus-more/27961

add_filter('x_breadcrumbs_data', 'post_parent_trail', 9999999, 2);

function post_parent_trail ($crumbs, $args) {
  if ( is_singular('post') ) {

    $category = get_the_category( get_the_ID() );
    if ( count( $category ) > 0 ) {
    $crumbs[1]['label'] = $category[0]->name;
    $crumbs[1]['url'] = get_category_link( $category[0]->term_id );
    
   unset($crumbs[2]); //to make sure category won't duplicate

    }
  }
  return $crumbs;
} 

add_filter('x_breadcrumbs_data', 'post_parent_trail', 9999999, 2);

	function post_parent_trail ($crumbs, $args) {
		if ( is_singular('post') ) {

			$category = get_the_category( get_the_ID() );

				if ( count( $category ) > 0 ) {

					$cat_array = array();
					$cat_array[1]['label'] = $category[0]->name;
					$cat_array[1]['url'] = get_category_link( $category[0]->term_id );

					array_splice($crumbs, 2, 0, $cat_array);
				}
		}
	return $crumbs;  
}

add_filter('x_breadcrumbs_data', 'post_parent_trail', 9999999, 2);

	function post_parent_trail ($crumbs, $args) {
		if ( is_singular('post') ) {

			unset($crumbs[1]);
				
		}
	return $crumbs;  
}

It doesn’t work for my site: http://dey.limit8design.com/blogs/denishas-blog/

I use no category base plugin and also added css code to block blog home page display in breadcrumbs:

.blog .x-breadcrumbs :nth-child(2),
.blog .x-breadcrumbs :nth-child(3),
.archive .x-breadcrumbs :nth-child(2),
.archive .x-breadcrumbs :nth-child(3),
.single-post .x-breadcrumbs :nth-child(2),
.single-post .x-breadcrumbs :nth-child(3) {
    display: none;
}

I want breadcrumbs look like the following:

home/category-name/post-title

and for categories that have subcategories I want breadcrumbs look like this:

home/category-name/subcategory-name/post-title

Please advise.

Thanks,

Yelena

Hi Yelena,

Thanks for reaching out.

It should already be working like that without a need for code, except for sub-category. This code should add a category by first level (but remove all the added code from above).

function my_custom_breadcrumbs( $crumbs, $args ) {

$crumb_index = false ;

foreach ( $crumbs as $i => $crumb ) {
    if ( strtolower( $crumb['label'] ) === 'blogs' && is_singular('post') ) {

       $crumb_index = $i + 1; //prevent zero for splicing
       break;

    }
}


if( $crumb_index ) {

    $category = get_the_category( get_the_ID() );

    if ( count( $category ) > 0 ) {
    
    return array_merge ( array_slice($crumbs, 0, $crumb_index, true), array( array( 'label' => $category[0]->name, 'url' => get_category_link( $category[0]->term_id ), 'type' => 'archive' ) ), array_slice($crumbs, $crumb_index, count($crumbs)-$crumb_index, true) );

    }
	

}

return $crumbs;

}



add_filter( 'x_breadcrumbs_data', 'my_custom_breadcrumbs', 10, 2 );

Adding another level of category or more is complex and would require more codes since you have to insert them to existing breadcrumbs. But the code should give you the idea on how to implement it, but starting with a simple one. If you play configuring your breadcrumb completely different from what’s theme provided then I recommend existing breadcrumb feature instead of re-coding it over. Example is the SEO Yoast’s breadcrumb, please check this https://theme.co/apex/forum/t/yoast-breadcrumbs/46946/8

Thanks!

Hi Rad and thanks. Unfortunately, it still doesn’t work.

My blog home page is set to ‘blog’ and it shows up on all index and post pages. I do NOT want to display it! Instead, I want the breadcrumbs look like this: home (icon)/category-name/post-title on all post pages.

Please advise.

Thanks,

Yelena

Have renamed ‘Blog’ to ‘All’ – blog home page – so it looks logical and okay, no need to remove the blog home from the breadcrumbs

Hi Yelena,

This strtolower( $crumb['label'] ) === 'blogs' part assume your blog home page has Blogs name which is based on actual post breadcrumb. But glad it’s okay now, cheers!

Thanks!

Hi Rad,

Please read my comments above carefully. Nothing has been resolved so far. I’ve found a work around the blog home ‘Blog’ by renaming it to ‘All’, but that’s pretty much it. What I need is for the breadcrumbs to show the full trail: Home -> All->Blogs->Post Name for every individual blog post page (every post under Blogs category), or
Home->All->Resources->Nancy’s Page->Post Name for the Resources category and Nancy’s Page subcategory.

Also, I couldn’t figure out how to get categories displayed next to the date in the blog masonry format. What I mean is that I want the standard blog meta information be displayed for each post on a category page. For whatever the reason, the theme seems to be created in a way that significantly limits the standard WP customization options.

Please advise.

Thanks,

Yelena

Hi Yelena,

In that case, pleas check the other thread that I provided about Yoast. The above code was just simply to add a single category before the post title given that the root breadcrumb title is blog or blogs. It’s not gonna work on your setup given that it has multiple layers and names.

Thanks!

Thanks, got it.

What about the second part of my question – post meta to display category name for posts on a category page?

Hi Yelena,

It would require customization too, the current stack you have selected doesn’t have that post meta feature for blog post. This is the code responsible for that,

  function x_icon_entry_meta() {

    $date = sprintf( '<span><time class="entry-date" datetime="%1$s">%2$s</time></span>',
      esc_attr( get_the_date( 'c' ) ),
      esc_html( get_the_date() )
    );

    if ( x_does_not_need_entry_meta() ) {
      return;
    } else {
      printf( '<p class="p-meta">%s</p>',
        $date
      );
    }

  }

But, you can change that, example, you can add this code to your child theme’s functions.php

  function x_icon_entry_meta() {

    $date = sprintf( '<span><time class="entry-date" datetime="%1$s">%2$s</time></span>',
      esc_attr( get_the_date( 'c' ) ),
      esc_html( get_the_date() )
    );

   if( is_category() ) {

     $categories        = get_the_category();
      $separator         = ', ';
      $categories_output = '';
      foreach ( $categories as $category ) {
        $categories_output .= '<a href="'
                            . get_category_link( $category->term_id )
                            . '" title="'
                            . esc_attr( sprintf( __( "View all posts in: &ldquo;%s&rdquo;", '__x__' ), $category->name ) )
                            . '"> '
                            . $category->name
                            . '</a>'
                            . $separator;
      }

      $categories_list = sprintf( '<span>%1$s %2$s',
        __( 'In', '__x__' ),
        trim( $categories_output, $separator )
      );

printf( '<p class="p-meta">%s</p>',
       $categories_list 
      );

   } else {
      printf( '<p class="p-meta">%s</p>',
        $date
      );
   }

  }

This is just the idea, it’s up to you how you’ll enhance it :slight_smile:

Thanks!

Thank you!

You are most welcome. :slight_smile:

This topic was automatically closed 10 days after the last reply. New replies are no longer allowed.