Site logo

Archived topic

Parent and custom post breadcrumb

2 replies · Started by _blank on September 6, 2018

Viewing posts 1–3 of 3

Hi

I’ve created a custom post type called “Collaborations” and would like to make the “about” page the Parent so that in the yoast breadcrumb it looks like this

HOME / ABOUT / COLLABORATIONS
Rather than HOME / COLLABORATIONS

I tried this code below but don’t understand it properly, is this something you can help with?

add_filter('wpseo_breadcrumb_links', 'add_my_ancestors');
function add_my_ancestors($links) {
    if (get_post_type() == 'collaborations') {
        $parent = get_post_meta(get_the_ID(), '_wpcf_belongs_about_id', true);
        array_splice($links, sizeof($links) - 1, 0, array(array('id' => $parent)));
    }
    return $links;
}

I’m using Toolset plugin for the post types. I’ve had some support from Toolset they suggested this snippet.

https://toolset.com/forums/topic/2-custom-post-types-can-one-be-the-parent-of-the-other-and-display-breadcrumbs/#post-34778

This is their instructions

you would alter the code thereby changing employee to the slug of your post type.
Then, in $parent = get_post_meta(get_the_ID(), '_wpcf_belongs_company_id', true); you would have to manipulate _wpcf_belongs_company_id and replace company with the slug of the related post type (parent type).

That line will get the ID of the single Post (companies in this example) that is related to the current Post (employee in the example) (hence, is it's parent).

Then, with array_splice($links, sizeof($links) - 1, 0, array(array('id' => $parent)));
it actually populates the Bradcrumbs using the Post ID of $parent (the related post)

Since that plugin offers a filter for their breadcrumbs you can hook the above new data to that output with add_filter('wpseo_breadcrumb_links', 'your_custom_callback');

So the whole code is:

add_filter('wpseo_breadcrumb_links', 'your_custom_callback');
function your_custom_callback($links) {
    if (get_post_type() == 'your-post-type-slug-of-child-type') {
        $parent = get_post_meta(get_the_ID(), '_wpcf_belongs_your-post-type-slug-of-child-type_id', true);
        array_splice($links, sizeof($links) - 1, 0, array(array('id' => $parent)));
    }
    return $links;
}

Many Thanks
Scott

Hey Scott,

Sorry for not getting back to you sooner!

I'm afraid I wouldn't really know as it involves two plugins I haven't used much. I assume you've updated the necessary parts of the code to match your post type?

Hi Tom

I understand, it's a bit of a mixed bag.
Don't think i've set it up properly.

Thanks
Scott

This archived topic is closed to new replies.