I am trying to add two levels to my breadcrumbs. I currently have:
Home > CPT Post
What I should have (or want to have):
Home > Page 1 > Page 2 > CPT Post
So I need to inject 2 links into what I have now.
Using this as a start...:
add_filter( 'wpseo_breadcrumb_links', 'yoast_seo_breadcrumb_append_link' );
function yoast_seo_breadcrumb_append_link( $links ) { global $post;
if ( is_single ( 123456 ) ) {
$breadcrumb[] = array(
'url' => site_url( '/blog/' ),
'text' => 'Blog',
);
array_splice( $links, 1, -2, $breadcrumb );
}
return $links;
}
...I get how I would build it out but am just not good enough yet to pop this in there.
Can you spell it out for me? Example is helpful for learning (for me and others). I really appreciate the help.
I am trying to add two levels to my breadcrumbs. I currently have:
Home > CPT Post
What I should have (or want to have):
Home > Page 1 > Page 2 > CPT Post
So I need to inject 2 links into what I have now.
Using this as a start...:
add_filter( 'wpseo_breadcrumb_links', 'yoast_seo_breadcrumb_append_link' );
function yoast_seo_breadcrumb_append_link( $links ) { global $post;
if ( is_single ( 123456 ) ) {
$breadcrumb[] = array(
'url' => site_url( '/blog/' ),
'text' => 'Blog',
);
array_splice( $links, 1, -2, $breadcrumb );
}
return $links;
}
...I get how I would build it out but am just not good enough yet to pop this in there.
Can you spell it out for me? Example is helpful for learning (for me and others). I really appreciate the help.
My quick and dirty solution was to nest 2 arrays and loop through them. You'll need to hard-code your nth level URLs. This example singles out a specific custom post type.
function yoast_seo_breadcrumb_append_link( $links ) {
global $post;
if ((is_singular('team-members'))) {
$breadcrumbs[] = array(
array(
'url' => site_url('/our-firm/'),
'text' => 'Our Firm'
),
array(
'url' => site_url('/our-firm/our-professionals/'),
'text' => 'Our Professionals'
)
);
foreach ($breadcrumbs as $breadcrumb) {
array_splice($links, 1, -2, $breadcrumb);
}
}
return $links;
}