Category and Subcategory Breadcrumbs Swapped Order

Howdy, I found this wonderful snippet in a previous post, and it almost works perfectly.

// Categories and Subcategories in breadcrumb
// =============================================================================
add_filter('x_breadcrumbs_data', 'post_parent_trail', 9999999, 2);
function post_parent_trail ($crumbs, $args) {
  if ( is_singular('product') ) {
    global $product;
    $category = get_the_terms( $product->ID, 'product_cat' );
    
    if ( count( $category ) > 0 ) {
      $cat_array             = array();
      $count = 0;
      foreach( $category as $cat ) {

        $cat_array[$count]['label'] = $cat->name;
        $cat_array[$count]['url']   = get_term_link( $cat->term_id );
        $count++;
      }

      $cat_array = array_reverse($cat_array);

      array_splice($crumbs, 2, 0, $cat_array);
    }
  }
  return $crumbs;  
}
// =============================================================================

But for some reason it swaps the location of the category with subcategory.
Any ideas on how to fix?

Example URL: https://usatcorp.com/shop/airlink-shark-fin-mag-mount-adapter-6001112/

Thank you,
Jesse @ USAT

1 Like

Hello Jesse,

Thanks for posting in!

The category array has been reversed. You will have to update the code from:
$cat_array = array_reverse($cat_array);

into this:
$cat_array = array_reverse($cat_array, TRUE);

For more details, you can check this out:

We would love to know if this has worked for you. Thank you.

1 Like

Awesome, worked like a charm thank you

Glad that we could be of help.

Cheers!

Sorry, i just noticed it’s still weird in some places.

Any ideas?

Hey Jesse,

I did some local tests and you probably don’t need the following line of code:

$cat_array = array_reverse($cat_array, TRUE);

You can simply remove this line of code from your snippet and it should work fine.

Please note that the code snippet provided in the forum serves as a guide only and is to help you in getting started, further customization from here would be getting into custom development, which is outside the scope of support we can offer. If you need more in depth changes, you may wish to consult with a developer.

Thank you for understanding!

PERFECT, thank you

You’re most welcome.

Cheers!

This topic was automatically closed 10 days after the last reply. New replies are no longer allowed.