Product Breadcrumbs to use Primary Category

Hi, I am using the below code snippet for adding category breadcrumbs as found on this forum and it is working well, apart from it seems to pull through the top category alphabetically rather than the primary category. Is there a way to set the primary category as the one that pulls through?

For example breadcrumbs would be HOME > SHOP > PRIMARY CATEGORY > PRODUCT

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();
			$cat_array[1]['label'] = $category[0]->name;
			$cat_array[1]['url']   = get_term_link( $category[0]->term_id );

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

Hello @chrisjdavies,

Thanks for writing in!

To get your issue resolved, please make use of the code given by @Thai on this old thread:

Kindly let us know how it goes.

Thanks for that, that seems to solve the issue. If I come across any problems I will be sure to let you know

Hi @chrisjdavies,

Glad that we are able to help you.

Thanks

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