Categories and Subcategories in Post Breadcrumbs

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;  
}

Hi Jesse,

Thank you for reaching out to us. As you see this requires custom development which is regretfully outside the scope of support we can offer. If you need more in depth changes to the code snippet, you may wish to consult with a developer. X is quite extensible with child themes, so there are plenty of possibilities.

Thank you for your understanding!

Is the reason that X theme is offering less and less support because there are now paid options? Because I’ve been using X theme for years now across several client accounts, and it used to be a rare occurrence when I was told the solution was “outside of scope”.

I finally found a post with a solution from March of 2018… that was “inside your scope” over two years ago…

in case anyone needs it, this is the combined script for adding breadcrumbs to both product and post page types.

// Categories and Subcategories in product and post 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);
    }
  }
  if ( is_singular('post') ) {
    $category = get_the_category( get_the_ID() );
    if ( count( $category ) > 0 ) {
      $cat_array = array();
      $cat_array[1]['label'] = $category[0]->name;
      $cat_array[1]['url'] = get_category_link( $category[0]->term_id );

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

@USAT009,

It’s our pleasure to help you.

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