• Resolved rafaviana

    (@rafaviana)


    Hi,
    Is it possible to set a page level limit for breadcrumbs? I have some pages where I don’t want to show all the parent categories:

    I this:
    ROOT PAGE > PARENT CATEGORY > CATEGORY > SUB-CATEGORY > PAGE

    I want this:
    ROOT PAGE > PARENT CATEGORY > CATEGORY > PAGE

    Is it possible to set a limit of 2 pages (parent category and category) ?

Viewing 4 replies - 1 through 4 (of 4 total)
  • Plugin Author John Havlik

    (@mtekk)

    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).

    Thread Starter rafaviana

    (@rafaviana)

    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 ?

    Plugin Author John Havlik

    (@mtekk)

    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]);
                }
             }
          }
       }
    }
    Thread Starter rafaviana

    (@rafaviana)

    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

Viewing 4 replies - 1 through 4 (of 4 total)

The topic ‘Breadcrumbs level limit’ is closed to new replies.