• Hi there,

    There is a simple issue that has been troubling me for quit some time now. On a Woocommerce product archive template I want to display the child categories of the top level category (which is a custom toxonomy actually).

    I’ve nailed it almost, the only thing I need is to get the id of the top level taxonomy in a variable. Anyone?

    This is what my code looks like.

    <?php
            $taxonomy     = 'product_cat';
            $orderby      = 'name';
            $order        = 'DESC';
    		$show_count   = 0;
            $pad_counts   = 0;
            $hierarchical = 1;
    
            $MY_TOP_LEVEL_ID = ?->term_id;
    
            $args2 = array(
              'taxonomy'     => $taxonomy,
              'child_of'     => $MY_TOP_LEVEL_ID,
              'show_count'   => $show_count,
              'pad_counts'   => $pad_counts,
              'hierarchical' => $hierarchical,
              'title_li'     => $title,
            );
    
            $sub_cats = get_categories( $args2 );
    
    		$cat_id = get_query_var('cat');
    		echo $cat_id;
    
    		if (!empty($sub_cats)) { 
    
    		echo '<ul class="categories">'; 
    
            foreach($sub_cats as $sub_category) {
    
    	        if ($sub_cats->$sub_category == 0) {
    	            $thumbnail_id  = get_woocommerce_term_meta( $sub_category->term_id, 'thumbnail_id', true );
    	            $image = wp_get_attachment_url( $thumbnail_id );
    				$name = $sub_category->name;
    				$slug = $sub_category->slug;
    
    	            echo '<li class="cat-item '.$slug.'"><a href="'. get_term_link($sub_category->slug, 'product_cat') .'"><img src="'.$image.'" " /><span class="txt">'.$name.'</span></a></li>';
    
    	            }
    	        }
    			echo '<li class="cat-item search">';
    			get_product_search_form();
    			echo '</li>';
    
            echo '</ul>';
    
    		 } //endif !empty ?>

    Thanks!

    [No bumping, thank you.]

  • The topic ‘Get id of top level taxonomy’ is closed to new replies.