Hello Abboutwe,
Following Snippet I have in the Header.php:
<?php if(!is_front_page()): ?>
<?php if ( function_exists(‘yoast_breadcrumb’) ) {
yoast_breadcrumb(‘<p id=”breadcrumbs”>’,'</p>’);
$breadcrumbs = yoast_breadcrumb(”,”,false);
} ?>
<?php endif; ?>
But with the Snippet in Header.php alone checking under google structured data testing the JSON LD Output (BreadcrumbList) is NOT shown.
To get the correct markup I have to add the following snippet to the functions.php:
add_filter(‘wpseo_breadcrumb_links’, ‘entex_add_crumb_schema’, 10, 1);
function entex_add_crumb_schema($crumbs) {
if( ! is_array( $crumbs ) || $crumbs === array()){
return $crumbs;
}
$last = count($crumbs);
$listItems = [];
$j = 1;
foreach ( $crumbs as $i => $crumb ) {
$item = [];
$nr = ($i + 1);
if(isset($crumb[‘id’])){
$item = [
‘@id’ => get_permalink($crumb[‘id’]),
‘name’ => strip_tags( get_the_title( $id ) )
];
}
if(isset($crumb[‘term’])){
$term = $crumb[‘term’];
$item = [
‘@id’ => get_term_link( $term ),
‘name’ => $term->name
];
}
if(isset($crumb[‘ptarchive’])){
$postType = get_post_type_object($crumb[‘ptarchive’]);
$item = [
‘@id’ => get_post_type_archive_link($crumb[‘ptarchive’]),
‘name’ => $postType->label
];
}
/* READ NOTE BELOW: */
if($nr == $last){
if(is_author() && !isset($crumb[‘url’])) $crumb[‘url’] = esc_url(get_author_posts_url(get_queried_object_id()));
}
/* The ‘text’ indicates the current (last) or start-path crumb (home)*/
if(isset($crumb[‘url’])) {
if($crumb[‘text’] !== ”) {
$title = $crumb[‘text’];
} else {
$title = get_bloginfo(‘name’);
}
$item = [
‘@id’ => $crumb[‘url’],
‘name’ => $title
];
}
$listItem = [
‘@type’ => ‘ListItem’,
‘position’ => $j,
‘item’ => $item
];
$listItems[] = $listItem;
$j++;
}
$schema = [
‘@context’ => ‘https://schema.org’,
‘@type’ => ‘BreadcrumbList’,
‘itemListElement’ => $listItems
];
$html = ‘<script type=”application/ld+json”>’ . stripslashes(json_encode($schema, JSON_PRETTY_PRINT)) . ‘</script> ‘;
echo $html;
remove_filter(‘wpseo_breadcrumb_links’, ‘entex_add_crumb_schema’, 10, 1);
return $crumbs;
}
Why is the JSON LD markup not shown just with the snippet in header.php, why do I have to add a extra code to functions to get the output shown correct, should this not be done from the Yoast Plugin iteslf?
Checked with Google Structured Data Tool my Content Pages, and see the BreadcrumbList Output just once present, where do you see it twice?
Thx
Best regards
Sally