Skip to content

Instantly share code, notes, and snippets.

@amboutwe
Last active August 23, 2024 01:58
Show Gist options
  • Save amboutwe/ea0791e184668a5c7bd7bbe357c598e9 to your computer and use it in GitHub Desktop.
Save amboutwe/ea0791e184668a5c7bd7bbe357c598e9 to your computer and use it in GitHub Desktop.
Multiple examples of how to customize the Yoast SEO breadcrumbs
<?php
/********* DO NOT COPY THE PARTS ABOVE THIS LINE *********/
/*
* Add shop link to the Yoast SEO breadcrumbs for a WooCommerce shop page.
* Credit: https://wordpress.stackexchange.com/users/8495/rjb
* Last Tested: Apr 20 2017 using Yoast SEO 4.6 on WordPress 4.7.3
*/
add_filter( 'wpseo_breadcrumb_links', 'wpseo_breadcrumb_add_woo_shop_link' );
function wpseo_breadcrumb_add_woo_shop_link( $links ) {
global $post;
if ( is_woocommerce() ) {
$breadcrumb[] = array(
'url' => get_permalink( woocommerce_get_page_id( 'shop' ) ),
'text' => 'Shop',
);
array_splice( $links, 1, -2, $breadcrumb );
}
return $links;
}
<?php
/********* DO NOT COPY THE PARTS ABOVE THIS LINE *********/
/*
* Remove breadcrumbs for specific items
* Credit: Yoast team
* Last Tested: Jul 21 2022 using Yoast SEO 19.3 on WordPress 6.0.1
*********
* DIFFERENT POST TYPES
* Post: Change 123456 to the post ID
* Page: Change is_single to is_page and 123456 to the page ID
* Custom Post Type: Change is_single to is_singular and 123456 to the 'post_type_slug'
Example: is_singular( 'cpt_slug' )
*********
* MULTIPLE ITEMS
* Multiple of the same type can use an array.
Example: is_single( array( 123456, 234567, 345678 ) )
* Multiple of different types can repeat the if statement
*/
add_filter( 'wpseo_breadcrumb_links', 'wpseo_breadcrumb_remove_limited' );
function wpseo_breadcrumb_remove_limited( $breadcrumbs ) {
if ( is_single ( 123456 ) ) {
return array(
'url' => '',
'text' => ''
);
}
else {
return $breadcrumbs;
}
}
<?php
/********* DO NOT COPY THE PARTS ABOVE THIS LINE *********/
/* Overwrite Rank Math breadcrumb shortcode with Yoast breadcrumbs
* Credit: Yoast team
* Last Tested: April 10, 2023 using Yoast SEO 20.4 on WordPress 6.2
* Rank Math must be inactive to overwrite the shortcode
*/
add_action('init', 'overwrite_rankmath_bc_shortcode_with_Yoast');
function overwrite_rankmath_bc_shortcode_with_Yoast() {
add_shortcode('rank_math_breadcrumb', 'my_custom_wpseo_breadcrumb');
}
function my_custom_wpseo_breadcrumb() {
return do_shortcode('[wpseo_breadcrumb]');
}
@amboutwe
Copy link
Author

This is not the proper place to request support. Please check out our extensive help section or visit the free support forum. If you require further support, upgrading to our premium version provides you with access to our support team.

@yhlee-op
Copy link

yhlee-op commented May 14, 2023

for people who need to update the url for the categories

// EDITING CATEGORY URL
add_filter( 'wpseo_breadcrumb_links', 'custom_breadcrumb_links' );

function custom_breadcrumb_links( $links ) {
    // Loop through the breadcrumb links array
    foreach ( $links as $index => $link ) {
        // Check if this is the CATEGORY
        if ( $link['text'] === 'Blog' ) {
            // Set the custom URL for breadcrumb
            $custom_url = 'https://example.com/blog';

            // Modify the link array with the custom URL
            $links[$index]['url'] = $custom_url;
			
        } elseif ( $link['text'] === 'Installers' ) {
			// Set the custom URL for breadcrumb
            $custom_url = 'https://example.com/installer';

            // Modify the link array with the custom URL
            $links[$index]['url'] = $custom_url;
			
		} elseif( $link['text'] === 'Competitors' ) {
			// Set the custom URL for breadcrumb
            $custom_url = 'https://example.com/competitor';

            // Modify the link array with the custom URL
            $links[$index]['url'] = $custom_url;
			
		}
    }

    return $links;
}

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment