Search code examples
wordpresscategoriestaxonomy

Hide the 'no category' in WordPress


I have a product page with a filter in it. I want to hide the 'no category' text if there is no category.

<?php wp_list_categories(array('taxonomy' => 'products', 'orderby' => 'order', 'title_li' => '', 'child_of' => ($term->parent==0) ? $term->term_id : $term->parent)); ?>

How can I implement it?


Solution

  • Add show_option_none to your arguments array and set it to an empty string:

    <?php wp_list_categories(array('show_option_none' => '', 'taxonomy' => 'products', 'orderby' => 'order', 'title_li' => '', 'child_of' => ($term->parent==0) ? $term->term_id : $term->parent)); ?>
    


    You might also want to rewrite your code a little so it's easier to debug and not one long line, e.g.:

    <?php 
    
    $args = array(
        'taxonomy' => 'products', 
        'orderby' => 'order', 
        'title_li' => '', 
        'child_of' => ( $term->parent == 0 ) ? $term->term_id : $term->parent
        'show_option_none' => '', 
    );
    
    wp_list_categories( $args ); 
    
    ?>