Take the 2-minute tour ×
WordPress Development Stack Exchange is a question and answer site for WordPress developers and administrators. It's 100% free, no registration required.

I need to show

  • MAIN term
    • sub term 1
    • sub term 2

Every time I click on sub term. I mean, I don't want to show the other MAIN terms, just the one I select with the sub terms.

In woocommerce-template.php I use:

function woocommerce_content() {

    if ( is_singular( 'product' ) ) {

        while ( have_posts() ) : the_post();?>

        <div class="three columns alpha">
            <div id="menu_commerce">
            <?php 
                    global $post;

                    $terms = get_the_terms( $post->ID, 'product_cat' );
                    foreach ( $terms as $term ){
                    $category_id = $term->term_id;
                    $category_name = $term->name;
                    $category_slug = $term->slug;
                    echo '<li><a href="'. get_term_link($term->slug, 'product_cat') .'">'.$category_name.'</a></li>';

                    }?>

            </div><!--END MENU COMMERCE-->
        </div><!--END 3 COLUMNS ALPHA-->

        <div class="thirteen columns alpha">

        <?php   woocommerce_get_template_part( 'content', 'single-product' );?>

        </div>

        <?php endwhile;

    } else {

        ?>

        <div class="three columns alpha">
            <div id="menu_commerce">
            <?php 

                global $wp_query;
                // get the query object
                $cat_obj = $wp_query->get_queried_object();
                if($cat_obj)    {
                    $category_name = $cat_obj->name;
                    $category_desc = $cat_obj->description;
                    $category_ID  = $cat_obj->term_id;
                    $category_slug = $cat_obj->slug;
                }
                woocommerce_subcats_from_parentcat_by_ID($category_ID)

                ?>
            </div><!--END MENU COMMERCE-->
        </div><!--END 3 COLUMNS ALPHA-->

        <div class="thirteen columns alpha">

        <div id="prod_titolo"><h1><?php woocommerce_page_title(); ?></h1></div>
        <hr style="margin-bottom:20px"/>
        <?php do_action( 'woocommerce_archive_description' ); ?>

        <?php if ( have_posts() ) : ?>

            <?php do_action('woocommerce_before_shop_loop'); ?>

            <div id="menu_grid">

            <?php woocommerce_product_loop_start(); ?>

                <?php woocommerce_product_subcategories(); ?>

                <?php while ( have_posts() ) : the_post(); ?>

                    <div class="four columns">

                    <?php woocommerce_get_template_part( 'content', 'product' ); ?>

                    </div><!--END 4 col-->

                <?php endwhile; // end of the loop. ?>

            <?php woocommerce_product_loop_end(); ?>
            </div><!--END menu grid-->

It works for single product, but if I click on a sub term, the MAIN disappears.

The function I use in functions.php is:

function woocommerce_subcats_from_parentcat_by_ID($parent_cat_ID) {
$args = array(
   'hierarchical' => 1,
   'show_option_none' => '',
   'hide_empty' => 0,
   'parent' => $parent_cat_ID,
   'taxonomy' => 'product_cat'
);
$subcats = get_categories($args);
echo '<ul class="wooc_sclist">';

  foreach ($subcats as $sc) {
    $link = get_term_link( $sc->slug, $sc->taxonomy );
      echo '<li><a href="'. $link .'">'.$sc->name.'</a></li>';
  }
echo '</ul>';

}

Can you help me please?

share|improve this question
    
just to clarify, you want to output the main-category (toplevel) on the sub-category archive pages, right? –  ialocin May 31 '13 at 10:08
    
yes! can you help me please? –  Yhis May 31 '13 at 10:54

1 Answer 1

up vote 1 down vote accepted

code

function wc_origin_trail_ancestor( $link = false, $trail = false ) {

    if (is_product_category()) {
        global $wp_query;
        $q_obj = $wp_query->get_queried_object();
        $cat_id = $q_obj->term_id;

        $descendant = get_term_by("id", $cat_id, "product_cat");
        $descendant_id = $descendant->term_id;

        $ancestors = get_ancestors($cat_id, 'product_cat');
        $ancestors = array_reverse($ancestors);

        $origin_ancestor = get_term_by("id", $ancestors[0], "product_cat");
        $origin_ancestor_id = $origin_ancestor->term_id;

        $ac = count($ancestors);

    } else if ( is_product() ) {

        $ancestors = get_the_terms( $post->ID, 'product_cat' );

        $i = 0;
        foreach($ancestors as $ancestor) {
            if( $i == 0 ) {
                $origin_ancestor = $ancestor;
                $origin_ancestor_id = $origin_ancestor->term_id;
            }
        }

        $ancestors = array_reverse($ancestors);

        $ac = count($ancestors);

        $descendant = $ancestors[0];
        $descendant_id = $descendant->term_id;

        $ancestors = array_reverse(get_ancestors($descendant_id, 'product_cat'));

    }


    $c = 1;
    if( $trail == false ){

        $origin_ancestor_term = get_term_by("id", $origin_ancestor_id, "product_cat");
        $origin_ancestor_link = get_term_link( $origin_ancestor_term->slug, $origin_ancestor_term->taxonomy );

        if($link == true) 
            echo '<a href="'. $origin_ancestor_link .'">';
        echo $origin_ancestor->name;
        if($link == true) 
            echo '</a>';

    }else{

        foreach ($ancestors as $ancestor) {
            $ancestor_term = get_term_by("id", $ancestor, "product_cat");
            $ancestor_link = get_term_link( $ancestor_term->slug, $ancestor_term->taxonomy );

            if($c++ == 1) 
                echo '» '; 
            else if($c++ != 1 || $c++ != $ac) 
                echo ' » ';

            if($link == true) 
                echo '<a href="'. $ancestor_link .'">';
            echo  $ancestor_term->name;
            if($link == true) 
                echo '</a>';

        }

        $descendant_term = get_term_by("id", $descendant_id, "product_cat");
        $descendant_link = get_term_link( $descendant_term->slug, $descendant_term->taxonomy );

        echo ' » ';
        if($link == true) 
            echo '<a href="'. $descendant_link .'">';
        echo $descendant->name;
        if($link == true) 
            echo '</a>';

    }

}

how to use

  • just toplevel, origin ancestor; without link
    wc_origin_trail_ancestor();
  • just toplevel, origin ancestor; with link
    wc_origin_trail_ancestor(true);
  • ancestor trail; without link
    wc_origin_trail_ancestor(false,true);
  • ancestor trail; with link
    wc_origin_trail_ancestor(true,true);

notes

  • won't work if a product has multiple main-/toplevel-categories, or at least won't show them all;
  • same should be the case for multiple subcategories on the same level;
  • I did no further tests concerning above points, because the function does all I wanted for that project;
share|improve this answer
    
wow! great! it's perfect! thanks a lot! I hope to help you some day as you've done with me! thanks again! –  Yhis May 31 '13 at 14:48
    
I'm glad i could help! Maybe you can help me sometime or just help somebody else! –  ialocin Jun 1 '13 at 10:07

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.