Well, there are a couple of ways you could achieve this. Using something like Order Bender to set a preferred category, and setting that to be the 2nd level category for each applicable post is one way of achieving this result. You could hook into the bcn_after_fill action, go through the breadcrumbs array and removing category breadcrumbs after there have been 2 (see https://mtekk.us/archives/guides/conditionally-remove-home/ for an example of removing a breadcrumb from the breadcrumb trail, you will just need more logic for figuring out what breadcrumb to remove).
Hi, I really appreciate your help. I took a look into the documentation but I don’t have the proper skills to write that code.
How can I go through the breadcrumbs array to remove category breadcrumbs after there have been 2 ?
The below code, in theory, probably does what you want. Note that I have not tested it so there may be a PHP warning/error message lurking in there.
add_action('bcn_after_fill', 'my_only_two');
function my_only_two($trail)
{
if(count($trail->breadcrumbs) > 4)
{
$breadcrumbs = $trail->breadcrumbs;
krsort($breadcrumbs);
$cat_count = 0;
foreach($breadcrumbs as $key => $breadcrumb)
{
if(in_array('category', $breadcrumb->get_types()))
{
$cat_count++;
if($cat_count >2)
{
unset($trail->breadcrumbs[$key]);
}
}
}
}
}
Hi! It works perfectly. I just had to change the custom post type ‘category’ to my custom post type slug ‘dt_portfolio_category’
Thank you