Snippet to modify Yoast SEO Breadcrumbs for post type parent
-
Please consider adding the following code to your plugin to use your plugin’s settings to modify the Yoast SEO breadcrumbs.
add_filter( 'wpseo_breadcrumb_links', function ( $links ) {
$custom_post_type_parents_options = get_option( 'custom_post_type_parents_options' );
if ( !empty( $custom_post_type_parents_options ) ) {
foreach( $custom_post_type_parents_options as $option_name => $value ) {
if ( !empty( $value ) ) {
$post_type = str_replace( 'parent-', '', $option_name );
if ( is_singular( $post_type ) ) {
// Add your landing page as the parent link
$landing_page_id = $value;
$landing_page = get_post( $landing_page_id );
$breadcrumbs = [];
if ( $landing_page->post_parent ) {
$parent = get_post( $landing_page->post_parent );
$breadcrumbs[] = [
'url' => get_permalink( $parent),
'text' => $parent->post_title
];
}
$breadcrumbs[] = [
'url' => get_permalink( $landing_page_id ),
'text' => $landing_page->post_title,
];
array_splice( $links, 1, 0, $breadcrumbs );
}
}
}
}
return $links;
});
- You must be logged in to reply to this topic.