php - Display all categories including sub categories under a list ul

admin2025-01-08  3

This code is displaying only categories, how can I change to also include subcategories. Thanks Thanks, with this code is listing all together, I want that shows like this

  • Category 1
    • Subcategory 1 within category 1
    • Subcategory 2 within category 1
    • Subcategory 3 within category 1
  • Category 2
<?php 
$args = array(
    'taxonomy'          => 'product_cat',
    'hide_empty'        => false,
    );
$result = get_terms( $args );

?>

<ul class="list-unstyled">

                            <?php
                            foreach ( $result as $cat ) {
                                if ( 'Uncategorized' !== $cat->name ) {
                                $term_link = get_term_link( $cat, 'product_cat' );
                                $cat_thumb_id = get_woocommerce_term_meta( $cat->term_id, 'thumbnail_id', true );
                                $shop_catalog_img_arr = wp_get_attachment_image_src( $cat_thumb_id, 'shop_catalog' );
                                $cat_img = $shop_catalog_img_arr[0];
                            ?>
                            
                            
                                <li><a href="<?php echo $term_link; ?>">
                                        <?php echo $cat->name; ?> 
                                    </a></li>
                                
                            

                        <?php
                            }
                        }
                        ?>

</ul>






This code is displaying only categories, how can I change to also include subcategories. Thanks Thanks, with this code is listing all together, I want that shows like this

  • Category 1
    • Subcategory 1 within category 1
    • Subcategory 2 within category 1
    • Subcategory 3 within category 1
  • Category 2
<?php 
$args = array(
    'taxonomy'          => 'product_cat',
    'hide_empty'        => false,
    );
$result = get_terms( $args );

?>

<ul class="list-unstyled">

                            <?php
                            foreach ( $result as $cat ) {
                                if ( 'Uncategorized' !== $cat->name ) {
                                $term_link = get_term_link( $cat, 'product_cat' );
                                $cat_thumb_id = get_woocommerce_term_meta( $cat->term_id, 'thumbnail_id', true );
                                $shop_catalog_img_arr = wp_get_attachment_image_src( $cat_thumb_id, 'shop_catalog' );
                                $cat_img = $shop_catalog_img_arr[0];
                            ?>
                            
                            
                                <li><a href="<?php echo $term_link; ?>">
                                        <?php echo $cat->name; ?> 
                                    </a></li>
                                
                            

                        <?php
                            }
                        }
                        ?>

</ul>






Share Improve this question edited Jul 5, 2020 at 2:03 Carlos asked Jul 5, 2020 at 0:30 CarlosCarlos 11 bronze badge 1
  • This code does exactly what you're asking for, it is shows all WooCommerce categories including subcategories. If you mean that subcategories should be displayed under another <ul> list level, you need a recursive function. – Ivan Shatsky Commented Jul 5, 2020 at 1:42
Add a comment  | 

1 Answer 1

Reset to default 1

Do you know how recursive functions are working? Here is an example:

function get_categories_list( $parent ) {
    $args = array(
        'taxonomy'          => 'product_cat',
        'hide_empty'        => false,
        'parent'            => $parent
    );
    $current_level_categories = get_terms( $args );
    $result = '';
    if ( $current_level_categories ) {
        foreach ( $current_level_categories as $cat ) {
            if ( $cat->name == 'Uncategorized' ) continue;
            $term_link = get_term_link( $cat, 'product_cat' );
            $cat_thumb_id = get_woocommerce_term_meta( $cat->term_id, 'thumbnail_id', true );
            $shop_catalog_img_arr = wp_get_attachment_image_src( $cat_thumb_id, 'shop_catalog' );
            $cat_img = $shop_catalog_img_arr[0];
            $result .= '<li><a href="' . $term_link . '">' . $cat->name . '</a>' . get_categories_list( $cat->term_id ) . '</li>';
        }
        $result = '<ul class="list-unstyled">' . $result . '</ul>';
    }
    return $result;
}
echo get_categories_list( 0 );

To filter "Uncategorized" category I'd better rely on its slug rather than on its name:

if ( $cat->slug == 'uncategorized' ) continue;
转载请注明原文地址:http://conceptsofalgorithm.com/Algorithm/1736270297a1418.html

最新回复(0)