After needing to add a breadcrumb to search results within a WordPress build and not finding much in the way of resources, I thought it would be useful to share what I had discovered, with the help of Darren Pinder.
How to add Yoast SEO breadcrumbs to search results
// Get breadcrumbs as an array from Yoast $post_breadcrumbs = YoastSEO()->meta->for_post( $post->ID )->breadcrumbs; $i = 0; // Count number of items in array $len = count($post_breadcrumbs); foreach ($post_breadcrumbs as $breadcrumb) { // If it is the last item in the array // remove the forward slash after if ($i == $len - 1){ echo $breadcrumb['text']; } else { // otherwise output just the text of the breadcrumb // followed by spaces and a forward slash echo $breadcrumb['text'].' / '; } $i++; }
I hope someone else finds this little snippet useful, as a way of improving the search results in WordPress when using the Yoast SEO plugin.