Hello,
I didn’t want the categories and subcategories to appear int the URL of posts, mainly because of SEO based URL length issues. I didn’t want it in the product URL’s either, but i did want it in the product breadcrumbs.
I found and worked through a solution with your team already for the product breadcrumbs.
Now i realize, I want the same solution, but for posts. I tried adding a new set of code to the functions.php that swapped “product” for “post” everywhere in the code, but an error occurred stating i can’t add two types of the same function.
i’m assuming, that i need to put it all in one set of code, to do both the product and post breadcrumbs. But all my attempts failed because i’m awful at scripting.
Can you help amend the code below?
// Categories and Subcategories in product breadcrumbs
// =============================================================================
//
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++;
}
array_splice($crumbs, 2, 0, $cat_array);
}
}
return $crumbs;
}