How can I display categories on different pages

admin2025-06-04  3

I am working on a theme. Woocommerce categories are displayed on home page but I need to add on shop page, product page and category page.

How can I do this.

I am working on a theme. Woocommerce categories are displayed on home page but I need to add on shop page, product page and category page.

How can I do this.

Share Improve this question asked Jan 24, 2019 at 10:07 Farrukh Saeed GhummanFarrukh Saeed Ghumman 11 bronze badge
Add a comment  | 

1 Answer 1

Reset to default 0

woocommerce is off topic here.

But following code should get all product categories ( sub categories as well ).

Just place it in the template ( where you want categories to appear in )

<?php

  $taxonomy     = 'product_cat';
  $hierarchical = 1; 
  $title        = '';  
  $empty        = 0;

  $args = array(
         'taxonomy'     => $taxonomy,
         'hierarchical' => $hierarchical,
         'title_li'     => $title,
         'hide_empty'   => $empty
  );
 $woo_categories = get_categories( $args );
 foreach ($woo_categories as $cat) {
    if($cat->category_parent == 0) {
        $cat_id = $cat->term_id;       
        echo '<br /><a href="'. get_term_link($cat->slug, 'product_cat') .'">'. $cat->name .'</a>';

        $args1 = array(
                'taxonomy'     => $taxonomy,
                'child_of'     => 0,
                'parent'       => $cat_id,
                'orderby'      => $orderby,
                'show_count'   => $show_count,
                'pad_counts'   => $pad_counts,
                'hierarchical' => $hierarchical,
                'title_li'     => $title,
                'hide_empty'   => $empty
        );
        $sub_categories = get_categories( $args1 );
        if($sub_categories) {
            foreach($sub_categories as $sub_category) {
                echo '<br /><a href="'. get_term_link($sub_category->slug, 'product_cat') .'">'. $sub_category->name .'</a>';
            }   
        }
    }       
}
    ?>
转载请注明原文地址:http://conceptsofalgorithm.com/Algorithm/1748979145a315338.html

最新回复(0)