skip to Main Content

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


  1. Please try below code.

    function remove_yoast_breadcrumb_link($link_output , $link)
    {
        $text_to_remove = 'Producten';
    
        if($link['text'] == $text_to_remove) {
          $link_output = '';
        }
    
        return $link_output;
    }
    add_filter('wpseo_breadcrumb_single_link' ,'remove_yoast_breadcrumb_link', 10 ,2);
    
    Login or Signup to reply.
  2. Based on the code provided by Yoast SEO, the correct way to make it work is something like:

    function remove_breadcrumbs_from_schema( $pieces, $context ) {
        return array_filter( $pieces, function( $piece ) {
            if( 'Producten' !== $piece->name ) {
                return $piece instanceof YoastWPSEOGeneratorsSchemaBreadcrumb;
            }
        } );
    }
    

    It should work this time.

    Login or Signup to reply.
Please signup or login to give your own answer.
Back To Top
Search