• Hi guys, i have the code below which displays all my categories and sub categories in list form, which works perfectly for what i want, but i would like to be able to choose a specific category to choose instead of them all – can this be done? (instead of showing all product_cat, show product_cat = “outdoors” )

    if ( is_front_page() ) {
    
    function get_product_terms( $term_id ) {
    
    		$html = '';
    
    		$args = array( 'hide_empty' => 0, 'parent' => $term_id );
    
    		$terms = get_terms('product_cat', $args);
    
    		foreach ($terms as $term) {
    
    			$html .= '<li';
    
    			if( $term_id == 0 ) {
    
    				$html .= ' class="top_li"';
    
    			}
    
    			$html .= '><a href="'.get_term_link($term->slug, 'product_cat').'">' . $term->name . '</a>';	
    
    			if( $list = get_product_terms( $term->term_id )) {
    
    				$html .= '<ul class="second_level">'.$list.'</ul>';
    
    			}
    
    			$html .= '</li>';
    
    		}
    
    		return $html;
    
    	}
    
    	if( $list = get_product_terms( 0 )) {
    
    		echo '<ul id="top_ul">'.$list.'</ul><div style="clear:both;">';
    
    	}
    
      global $post;
    
    }

    Thanks in advance for any help.

    http://wordpress.org/extend/plugins/woocommerce/

Viewing 2 replies - 1 through 2 (of 2 total)
  • Paul

    (@madebypaul)

    Just to clarify, you are looking to list all subcategories of a specific category? If so, then this function is already setup to do just that. SImply call this function and insert the term_id for the “outdoors” in the args for that function.

    For instance, if your “outdoors” category has a term_id of “3” your call to this function would look like this:

    <?php get_product_terms( 3 ); ?>

    It seems like the code you have listed here is setup to only run on the front page, so if you might want to copy this function and paste it with a different function name somewhere in your functions file so you can use it elsewhere.

    Thread Starter jayseventwo

    (@jayseventwo)

    Thanks for your reply. I only wanted it to display on the front page, but your idea is much better. Unfortunately, i have added it to my functions.php and am getting this error:

    Catchable fatal error: Object of class WP_Error could not be converted to string in /home/jayseven/public_html/woo/wp-content/themes/jayseventwo/lib/custom.php on line 22

    Line 22 is:
    $html .= '><a href="'.get_term_link($term->slug, 'product_cat').'">' . $term->name . '</a>';

    Any ideas?

    Also, would the category id also be the tag_id, as i cannot actually see that category id anywhere?

    I will keep on trying to resolve this issue, but if you have any ideas that would be great. Thanks.

Viewing 2 replies - 1 through 2 (of 2 total)
  • The topic ‘Display specific categories in list form – working code example provided’ is closed to new replies.