back to parent category (2 posts)

  1. numero7
    Posted 3 years ago #

    Hi im looking to add a "back to product category" button in my single product page with no success ( the link should send me back to the parent product category). I've tried the get category function with no luck...
    someone can help ?
    thanks a million...


  2. bheadrick
    Posted 3 years ago #

    um, isn't that what breadcrumbs are for?

    get_category just prints out link(s) to the applicable categories. You need to use get_terms to access the taxonomy objects and find the ones with no parent

    $terms = get_the_terms($post->ID,'product_cat');
    $count = count($terms); $i=0;
    if ($count > 0) {
        $term_list = '<span class="parent_category">Parent Category:';
        foreach ($terms as $term) {
        	$term_list .= '<a href="/product-category/' . $term->slug . '" title="' . sprintf(__('View all post filed under %s', 'my_localization_domain'), $term->name) . '">' . $term->name . '</a>';
        	if ($count != $i) $term_list .= ' &middot; '; }
        echo $term_list .'</span>';

Topic Closed

This topic has been closed to new replies.

About this Plugin

  • WooCommerce
  • Frequently Asked Questions
  • Support Threads
  • Reviews

About this Topic