php - Yoast Breadcrumbs Inject Multiple Levels

admin2025-01-07  3

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.

Share Improve this question asked May 25, 2020 at 3:35 WagonerWagoner 11 bronze badge
Add a comment  | 

1 Answer 1

Reset to default 0

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;
}
转载请注明原文地址:http://conceptsofalgorithm.com/Algorithm/1736257722a459.html

最新回复(0)