i have this code below to show links for my custom taxonomy on my custom posts.
I want it to show only the categories that have published posts in them. This mostly works but if i put all the posts in a certain category to draft, it still shows up as a link, but when a user clicks the link it goes to a 404 page because there are no active links on it.
How can i make it just show the link for a category if there are PUBLISHED posts and not if there are only drafts or if the category has no posts?
<?php
//list terms in a given taxonomy
$args = array( 'hide_empty=0' );
$terms = get_terms( 'product_category', $args );
if ( ! empty( $terms ) && ! is_wp_error( $terms ) ) {
$count = count( $terms );
$i = 0;
$term_list = '<div class="product-category-list">';
foreach ( $terms as $term ) {
$i++;
$term_list .= '<a class="activeunderline" href="' . esc_url( get_term_link( $term ) ) . '" alt="' . esc_attr( sprintf( __( 'View all post filed under %s', 'my_localization_domain' ), $term->name ) ) . '">' . $term->name . '</a>';
if ( $count != $i ) {
$term_list .= ' · ';
}
else {
$term_list .= '</div>';
}
}
echo $term_list;
}
?>
hide_empty
to false in your code, this will show terms whether or not they have posts assigned.