Modify Yoast SEO Breadcrumbs with Additional Item

Breadcrumbs provide user navigation functionality and SEO benefits, that cannot be missed. Yoast SEO plugin can help to implement breadcrumbs into your templates with a simple snippet and elaborate back-end settings and options.

Example template / header implementation can also check where to put it with wp conditionals.

<?php 
// exclude homepage
if ( ! is_front_page() ): ?>
<?php
    // check that breadcrumbs are enabled in Yoast settings
    if ( function_exists('yoast_breadcrumb') ) { ?>
    <section id="breadcrumb-section">
        <?php yoast_breadcrumb( '<div id="breadcrumbs">','</div>' ); ?>
    </section>  
    <?php }  ?>
<?php else: ?>
    <div id="breadcrumb-none"></div>
<?php endif; ?>

Adding Link or Item to Breadcrumbs

Despite the fact that almost all options are present in Yoast’s settings I had to add a link to last unlinked page name in my 3 levels breadcrumb. Some plugin was creating more pages under that parent page. Had to provide some easy way to parent page for users. Usually last item in breadcrumbs doesn’t have any link, as we already know.

So here’s the code snippet that was put into functions.php of my child-theme to modify breadcrumbs of such page. In my case it was Careers page used for Comeet job positions plugin.

// add link to careers because of comeet plugin sub pages
add_filter( 'wpseo_breadcrumb_links', 'add_to_yoast_breadcrumb' );
function add_to_yoast_breadcrumb( $links ) {
    global $post;
    // careers page id 3773 
    if ( is_page() && ( $post->post_parent == '3773' || is_page('3773') ) ) { 
        $breadcrumb[] = array(
            'url' => get_permalink( 'careers' ),
            // 'text' => 'Positions',
        );
        array_splice( $links, 3, -4, $breadcrumb );
    }
    return $links;
}

After that Careers page got its link. If I enable 'text' => 'Positions' back, breadcrumb will also show Positions at the end.

Breadcrumbs with 4 levels

If your breadcrumb, needed to be changed, is not on third level, you may modify digits here: array_splice( $links, 3, -4, $breadcrumb ); to find what fits you.

Share:
This entry was posted in Templates, WordPress and tagged . Bookmark the permalink.

Leave a Reply

Your email address will not be published. Required fields are marked *