• Resolved jelly_bean

    (@jelly_bean)


    I have a category list which displays as so:

    Categories: trousers, black, 12, casual

    However I would like to display the list as so:

    Type: trousers
    Colour: black
    Size: 12
    Style: casual

    I’ve searched and searched for an answer, but I can’t work out how to show the category list with titles for each category.

    Any advice would be appreciated.

    Thanks.

    https://www.ads-software.com/plugins/woocommerce/

Viewing 8 replies - 1 through 8 (of 8 total)
  • Where exactly you are trying to show in the categories?

    (Type,colour,size,style) are categories or product attributes?

    Thread Starter jelly_bean

    (@jelly_bean)

    They are categories. My category structure is:
    Type
    -trousers
    -skirts
    -jackets

    Size
    -10
    -12
    -14

    etc.

    Thread Starter jelly_bean

    (@jelly_bean)

    I’m getting somewhere

    <?php global $post;
    $args = array( 'taxonomy' => 'product_cat',);
    $terms = wp_get_post_terms($post->ID,'product_cat', $args);
    
        $count = count($terms);
        if ($count > 0) {
    
            foreach ($terms as $term) {
                echo '<div>';
    			echo $term->parent;
    			echo $term->name;
                echo '</div>';
    
          }
    
       }?>

    I just need to work out how to echo the term parent name rather than the id.

    I don’t think this is the optimal solution. You have duplicate products?
    By using product attributes you will be able to use filters to show them in the way you want.

    Thread Starter jelly_bean

    (@jelly_bean)

    Ah thank you. I don’t know why I didn’t do that in the first place. That makes a lot more sense. doh!

    You are welcome!

    Thread Starter jelly_bean

    (@jelly_bean)

    If anyone is interested in the answer to my original query here is the code:

    <?php global $post;
    		$args = array( 'taxonomy' => 'product_cat',);
    		$terms = wp_get_post_terms($post->ID,'product_cat', $args);
        	$count = count($terms);
        	if ($count > 0) {
            foreach ($terms as $term) {
                echo '<div>';
    			$term = get_term_by( 'slug', $term->slug, 'product_cat' );
    			$parent = get_term_by( 'id', $term->parent,  'product_cat' );
    			if($parent):
    		    echo $parent->name;
    			echo ': ';
    		endif;
    			echo $term->name;
                echo '</div>';
          }
       }?>
    Thread Starter jelly_bean

    (@jelly_bean)

    I’m just adding to this in case anyone wants more control over the categories. I’ve discovered I don’t actually want all of my categories to show up on my single post page.

    <?php
      global $post;
      $args = array(
      'parent' => 27
      );
      $terms = wp_get_post_terms( $post->ID, 'product_cat', $args );
      foreach( $terms as $term )$categories[] = $term->slug; {
      echo 'Country: '.$term->name;
    }?>

    Using this code I can call any category I want to and place it anywhere I want to.

Viewing 8 replies - 1 through 8 (of 8 total)
  • The topic ‘Show category list with titles’ is closed to new replies.