Hello, I would like to ask you if you can help me about the breadcrumbs path of a CPT I have recently developed.
1. Created the CPT
2. Created Custom Taxonomy
3. Assigned Parent/Child Categories to the Custom Posts
The result which I am expecting to see is the full path of the breadcrumb but it is showing only the CPT's main slug and aterwards the Custom Post Type's title. Images are attached for reference.
Please advice me if there is a way to make the full CPT path in the breadcrumbs area.
That's not quite what I was expecting, because it is displaying the breadcrumbs as Home > post type > post name and I had the impression from your question that it was displaying something like home > taxonomy term > post name and you wanted to expand taxonomy term to display the hierarchy of terms where relevant.
It seems like you have some overlap between the post type names and the taxonomy names, so I'm not sure what I'm looking at.
Can you clarify the breadcrumb structure you are aiming for, and perhaps I can take a look at your site to see for myself what the post types and taxonomies are.
I will mark your next reply as private so that I can get log-in credentials from you—you may want to create a temporary admin user for me to use that you can later delete. And be sure to have a current backup of your site, even though I don't intend to make any changes.
Hi, thank you. I have created a username and password from where you can login.
I try to display this kind of trail: /products > *parent category* > *child-category* ( and if possible if the child category has child category - on some posts is set - to display it after it too )
OK, I understand the requirements. This isn't really a Toolset issue (your theme is responsible for outputting this), but I'll take a look when I'm back in the morning, it shouldn't be too difficult to use get_terms to retrieve the terms, although there is one complication I notice.
On your site for the example Property Coverage post, you have only set the child term ("Property Coverage") and not the parent term(s), i.e. "Commercial insurance".
It will be somewhat more complicated if you don't apply the hierarchy of terms to the posts, is there some reason you cannot?
Hi Nigel. Thanks much for your help!
While I was adding content to the Post Types I was setting on the field ( attached image ) the current category and on the dropdown was selecting which will go for parent for this Category.
After updating the page the tick sign was applied only on the category which I set without having the parent category ticked.
Then I manually ( on a couple of Custom Posts ) assigned those ticks - which to be Primary Category and which to be Child ( as seen in the image attached ) Please advice if this is needed for me to make and I will scroll through the Post Types and tick the Primary categories on each post there to look exactly as in the image attached.
Thanks again for the help and hopefully we will get to resolve this.
Best Regards!
I don't recognise that last screenshot with terms marked as 'primary' and a 'Make primary' link, it's not standard WordPress, but it is also not what I see on your site if I edit a ProtectOne Product post (see my screenshot).
In any case, you need to make sure that the whole hierarchy of terms is assigned to the post for this to work.
You'll need to edit the theme template and replace the whole block of code that produces the breadcrumbs with the following:
global $post;
// Produce an array with the taxonomy hierarchy
$hierarchy = _get_term_hierarchy('protectone-categories');
$lastEl = array_values(array_slice($hierarchy, -1))[0];
$hierarchy = array_keys($hierarchy);
$hierarchy[] = $lastEl[0];
// Get the assigned terms in the correct order
$terms = get_terms(array(
'taxonomy' => 'protectone-categories',
'orderby' => 'include',
'object_ids' => $post->ID,
'include' => $hierarchy,
));
$home = get_home_url();
// Build the html for the breadcrumbs
$output = "<a href='" . $home . "'>Home</a>";
foreach ($terms as $key => $term) {
$link = get_term_link($term);
$output .= " / <a href='" . $link . "'>" . $term->name . "</a>";
}
$output .= " / " . $post->post_title;
// Print the breadcrumbs
echo $output;
This needs to be inside php tags.
It will output the breadcrumbs in the format:
Home link > parent category link > child category link > grandchild category link > post title
Note that I didn't try and reproduce adding classnames etc. to the links, I'll leave that to you if you need it.