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
<?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
<?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>
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;
<ul>
list level, you need a recursive function. – Ivan Shatsky Commented Jul 5, 2020 at 1:42