By using a code snippet I try to remove a schema output from Yoast for breadcrumbs but I can’t seem to fix it. This is the code provided by Yoast SEO:
// functions.php
add_filter( 'wpseo_schema_graph_pieces', 'remove_breadcrumbs_from_schema', 11, 2 );
add_filter( 'wpseo_schema_webpage', 'remove_breadcrumbs_property_from_webpage', 11, 1 );
/**
* Removes the breadcrumb graph pieces from the schema collector.
*
* @param array $pieces The current graph pieces.
* @param string $context The current context.
*
* @return array The remaining graph pieces.
*/
function remove_breadcrumbs_from_schema( $pieces, $context ) {
return array_filter( $pieces, function( $piece ) {
return ! $piece instanceof YoastWPSEOGeneratorsSchemaBreadcrumb;
} );
}
This is the snippet I use:
function remove_breadcrumbs_from_schema( $pieces, $context ) {
return array_filter( $pieces, function( $piece ) {
// Modify the condition to exclude the specific listitem based on its name
return ! (
$piece instanceof YoastWPSEOGeneratorsSchemaListItem &&
$piece->name === 'Producten'
);
} );
}
(And I tried many small changes here)
This is the output and the part I want to have deleted is this "{"@type":"ListItem","position":2,"name":"Producten","item":"https://ferrygogo.com/nl/winkel/"}" in the following schema output:
{"@type":"BreadcrumbList","@id":"https://ferrygogo.com/nl/overtocht/eemshaven-kristiansand/#breadcrumb","itemListElement":[{"@type":"ListItem","position":1,"name":"FerryGoGo","item":"https://ferrygogo.com/nl/"},{"@type":"ListItem","position":2,"name":"Producten","item":"https://ferrygogo.com/nl/winkel/"},{"@type":"ListItem","position":3,"name":"Noorwegen","item":"https://ferrygogo.com/nl/noorwegen/"},{"@type":"ListItem","position":4,"name":"Eemshaven-Kristiansand"}]}
I tried many things, I just want to delete the second position. So far I haven’t seen results on this as I keep on seeing that listitem appearing in the schema markup in the source.
2
Answers
Please try below code.
Based on the code provided by Yoast SEO, the correct way to make it work is something like:
It should work this time.