5.1 RC2 - Link within the breadcrumbs element

I doubt this has anything to do with the beta, but just noticed it…

On a site I’m building, I have the permalinks for posts set to use /%category%/%postname%/ for the URL. However, despite this, when I click the breadcrumb for the current post, it takes me to ?post_type=post&p=544. This does redirect to the correct place, of course, but it strikes me as odd that it doesn’t respect the permalink URL structure.

Thanks Devin,

I just tried this out and it’s working ok here. We’re just using the get_permalink function on the current post to output that link, so for some reason WordPress decided to output that instead of the right URL. Is the post in a draft state? That’s the only thing that comes to mind why the URL might be different.

Thanks @alexander! Now I see what was wrong :man_facepalming:

I wanted to also show the category in the breadcrumbs, so I had included the snippet shared by support in this thread.

However, that seems to be what caused the issue. That snippet had get_post_permalink in there for the post’s URL. When I switched that to get_permalink, it all started working as expected. So I think I’m squared away now!

BTW, here’s the full snippet in case there’s anything you’d suggest changing about it.

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[] = array(
        'type'  => 'category',
        'url'   => get_category_link( $category[0]->term_id ),
        'label' => $category[0]->name,
      );
	  $crumbs[] = array(
        'type'  => 'post',
        'url'   => get_permalink( get_the_ID() ),
        'label' => get_the_title(get_the_ID()),
      );
    
	unset($crumbs[2]); //to make sure category won't duplicate

    }
  }
  return $crumbs;
}

Gotcha! That should do it. Code looks great! I think get_post_permalink is meant for cases when you explicitly want that ugly version of the URL.