Wordpress - Show all terms of a custom taxonomy?
You need to pass an additional argument to get_terms()
. The default is to hide "empty" terms-- terms which are assigned to no posts.
$terms = get_terms([
'taxonomy' => $taxonomy,
'hide_empty' => false,
]);
Since 4.5.0, taxonomies should be passed via the ‘taxonomy’ argument in the $args array so:
$terms = get_terms( array(
'taxonomy' => 'post_tag',
'hide_empty' => false,
) );
where terms that have no posts are hidden by default.
This code is fetches all category and subcategory custom taxonomies using get_terms()
:
<?php $wcatTerms = get_terms('product_cat', array('hide_empty' => 0, 'parent' =>0));
foreach($wcatTerms as $wcatTerm) :
?>
<ul>
<li>
<a href="<?php echo get_term_link( $wcatTerm->slug, $wcatTerm->taxonomy ); ?>"><?php echo $wcatTerm->name; ?></a>
<ul class="megaSubCat">
<?php
$wsubargs = array(
'hierarchical' => 1,
'show_option_none' => '',
'hide_empty' => 0,
'parent' => $wcatTerm->term_id,
'taxonomy' => 'product_cat'
);
$wsubcats = get_categories($wsubargs);
foreach ($wsubcats as $wsc):
?>
<li><a href="<?php echo get_term_link( $wsc->slug, $wsc->taxonomy );?>"><?php echo $wsc->name;?></a></li>
<?php
endforeach;
?>
</ul>
</li>
</ul>
<?php
endforeach;
?>