0
votes

I am working on a scenario where there are multiple parent categories and each parent has multiple subcategories.

I have already set the parent categories to show subcategories and not products.

So here is my current hierarchy:

Parent Category 1

  • Subcategory 1
  • Subcategory 2
  • Subcategory 3

Parent Category 2

  • Subcategory 4
  • Subcategory 5
  • Subcategory 6

Everything is working fine but, I want to do one additional thing which shows the descriptions of all subcategories when they are being showed under parent category.

I know the title of subcategory is showed by default when viewing parent category but, I cannot find a way to override the default loop to show descriptions of subcategory in parent category.

Any help would be appreciated!

Thanks!

Update:

I have succeeded to show the subcategory name and description on parent category page with the following code:

if ( is_product_category() ) {

    $term_id  = get_queried_object_id();
    $taxonomy = 'product_cat';

    // Get subcategories of the current category
    $terms    = get_terms([
        'taxonomy'    => $taxonomy,
        'hide_empty'  => true,
        'parent'      => get_queried_object_id()
    ]);

    $output = '<ul class="subcategories-list">';

    // Loop through product subcategories WP_Term Objects
    foreach ( $terms as $term ) {
        $term_link = get_term_link( $term, $taxonomy );

        


        echo '<h3'. $term->slug .'"><a href="'. $term_link .'">'. $term->name .'</a></h3>';

        echo '<p class="'. $term->slug .'">'. $term->description .'</a></p>';
        
    
    }
    echo '</ul';
    
    
}

But, I am getting trouble in fetching images of subcategories. So any help in this regard would be super useful.

1
Please add some code. what you have tried and what you got instead . So the community better understand your questionJoy Kumar Bera
@JoyKumarBera updated with codeAhmad Wahid

1 Answers

0
votes

Modify your foreach loop like this

foreach ( $terms as $term ) {
        $term_link = get_term_link( $term, $taxonomy );
        $thumb_id = get_term_meta( $term->term_id, 'thumbnail_id', true );
        $thumb_src = wp_get_attachment_image_src( $thumb_id );

        echo '<h3'. $term->slug .'"><a href="'. $term_link .'">'. $term->name .'</a></h3>';

        echo '<p class="'. $term->slug .'">'. $term->description .'</a></p>';

        echo '<img src="'.$thumb_src[0].'">';
}