Breadcrumbs Not Displaying Post Title

Hello,

I am trying to get breadcrumbs to display Home > Category > Post.

I’ve added the following code provided by support to the functions.php file in the child theme. Breadcrumbs are now displaying Home > Category, yet they are still not displaying the post name.

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;
}

How can I modify this code to include the Post Name at the end?

If you need, you can see what is happening at https://www.spiritualexcellence.com/blog/.

Thank you :slight_smile:

Hi @etorabi,

Thanks for reaching out.

That only adds the category, to add the post title then please change it to this

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

}

$last_index = count( $crumbs );
$crumbs[$last_index]['label'] = get_the_title();
$crumbs[$last_index]['url'] = get_permalink();

}
return $crumbs;
}

Though, I don’t see any category in your breadcrumbs.

Thanks!

Thank you, that fixed it!

You’re welcome.

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