I am trying to get all categories which are having products but getting also the categories which are having no products.
WordPress version 4.6.1
wp_dropdown_categories(
array(
'class' => 'product-category-field',
'id' => 'product-category',
'name' => 'category',
'taxonomy' => 'product_cat',
'selected' => get_query_var('product_cat' ),
'hierarchical' => 1,
'hide_empty' => 1,
'value_field' => 'slug',
'show_count' => 1
)
);
Even get_terms
is displaying empty categories with the below code.
<?php $terms = get_terms('product_cat', array( 'parent' => 0 ));
if( $terms ):
$original_query = $wp_query;
foreach ( $terms as $key => $term ):
?>
<li>
<?php echo $term->name; ?>
<ul>
<?php
$child_terms = get_terms(
'product_cat',
array(
'child_of' => $term->term_id,
'hide_empty' => true
)
);
foreach ( $child_terms as $child_term ) {
$re_child_terms = get_terms(
'product_cat',
array(
'child_of' => $child_term->term_id,
'hide_empty' => true
)
);
if ( ! $re_child_terms ){
?>
<li>
<?php echo $child_term->name; ?>
</li>
<?php
}
}
?>
</ul>
</li>
<?php
endforeach;
$wp_query = null;
$wp_query = $original_query;
?>
</ul>
<?php endif; ?>
Note: In both case do not want to display categories having zero products.
wp_dropdown_categories()
, we can usewc_dropdown_categories()
and it will be done, but still no success withget_terms()
. – Prafulla Kumar Sahu Oct 14 '16 at 6:28