I’m looking for a way to list all the terms from a custom taxonomy. Only the terms that have posts attached to it should have links to the archive page. If there are no posts attached it should only show the names.
Any ideas? Thanks!
<?php
$taxonomy = 'cat';
$queried_term = get_term_by( 'slug', get_query_var($taxonomy) );
$terms = get_terms($taxonomy);
if ( $terms !== 0 ) {
foreach ( $terms as $term ) {
echo $term->name . ", ";
}
}
if ( $terms > 0 ) {
foreach ( $terms as $term ) {
echo '<li><a href="' . $term->slug . '">' . $term->name .'</a></li>';
}
}
?>
3 Answers
I didn’t understood your question well, but try this. Explanation is in the comments.
// your taxonomy name
$tax = 'post_tag';
// get the terms of taxonomy
$terms = get_terms( $tax, [
'hide_empty' => false, // do not hide empty terms
]);
// loop through all terms
foreach( $terms as $term ) {
// if no entries attached to the term
if( 0 == $term->count )
// display only the term name
echo '<h4>' . $term->name . '</h4>';
// if term has more than 0 entries
elseif( $term->count > 0 )
// display link to the term archive
echo '<h4><a href="'. get_term_link( $term ) .'">'. $term->name .'</a></h4>';
}
Hope it helped you.