Puddinq.com sharing knowledge

Insert extra link in yoast breadcrumb

Insert extra link in yoast breadcrumb

There are a few filters in WordPress to give you the oppertunity to change an aspect, but the variable is an array the has a cetrain order. Yoast breadcrums is a filter like that. If you want to add an extra link to the main blog at the second position it is not as simple as just adding that link to the end. The first example puts the blog title to the third position and the main blog link to the first position.

add_filter('wpseo_breadcrumb_links', function ($breadcrumbs) {

    if (is_singular('post') === true) {
        $breadcrumbs[3] = $crumbs[1]; // move the title to third position
        $breadcrumbs[1] = ['id' => 6];// add link to page with id
    }

    return $breadcrumbs;
}, 100);

and i love array slice, which just inserts the same link at the same position.


add_filter('wpseo_breadcrumb_links', function( $links ) {
    global $post;
    if ( is_single ( 123456 ) ) {
        $breadcrumb[] = array(
            'url' => site_url( '/blog/' ),
            'text' => 'Blog',
        );
        array_splice( $links, 1, -2, $breadcrumb );
    }
    return $links;
}
);