Special offer. See more information about Outbyte and uninstall instructions. Please review EULA and Privacy policy.

Some links on this page are affiliate links: if you buy through them we may earn a commission, at no extra cost to you.

For a standard category list, WordPress already hides categories with no posts. In a PHP theme template, make that behavior explicit with 'hide_empty' => true in wp_list_categories(). If you also want the heading and surrounding section to disappear when no categories qualify, retrieve the categories first and render the section only when the returned array is non-empty.

Hide empty categories in a PHP category list

Use wp_list_categories() to output category links. Its hide_empty argument defaults to true; setting it explicitly documents the intended behavior in your template. The title_li argument removes the function’s list-title element so your theme can provide its own heading.

<ul>
    <?php
    wp_list_categories(
        array(
            'hide_empty' => true,
            'title_li'   => '',
        )
    );
    ?>
</ul>

This filters the category links, but the surrounding <ul> or a heading written outside the function can still appear when there are no qualifying categories. The WordPress wp_list_categories() reference documents its arguments and output behavior.

Special offer. See more information about Outbyte and uninstall instructions. Please review EULA and Privacy policy.

Hide the heading and wrapper when no categories qualify

Call get_categories() and put the entire section inside a check for a non-empty result. The function returns an empty array when there are no categories to display.

<?php
$categories = get_categories(
    array(
        'hide_empty' => true,
    )
);

if ( ! empty( $categories ) ) {
    echo '<section class="category-navigation">';
    echo '<h2>' . esc_html__( 'Categories', 'textdomain' ) . '</h2>';
    echo '<ul>';

    foreach ( $categories as $category ) {
        printf(
            '<li><a href="%s">%s</a></li>',
            esc_url( get_category_link( $category->term_id ) ),
            esc_html( $category->name )
        );
    }

    echo '</ul>';
    echo '</section>';
}
?>

Keep the escaping functions when outputting term names and links. The WordPress get_categories() reference describes the function’s return value and query arguments.

Use the Categories List block without code

In the block editor, add a Categories List block and turn off its option to show empty categories. The block also offers options such as displaying post counts and limiting the list to top-level categories. See the WordPress Categories List block documentation for the available controls.

Choose the right meaning of “has posts”

The default empty-term filtering concerns posts assigned to a category. Do not assume that this is identical in every setup to the stricter condition “has published posts of this post type.” Post statuses, custom post types, and taxonomy registration can affect what a term query represents. The WordPress get_terms() reference documents the hide_empty behavior; verify it against your site if the published-post condition matters.

Special offer. See more information about Outbyte and uninstall instructions. Please review EULA and Privacy policy.

For a custom taxonomy, query the intended taxonomy rather than assuming the built-in post category taxonomy. Confirm that the taxonomy is registered for the content type whose posts you want counted.

Independent reader supportYour contribution helps us test, update, and keep practical guides available for everyone.Support on Ko-Fi

A category list is not a category archive

wp_list_categories() creates navigation links; selecting one takes a visitor to that category’s archive. Conditional tags such as is_category() test whether the current request is a category archive. They do not, by themselves, test whether an arbitrary category in a navigation list has content. See the WordPress conditional tags documentation and the Posts Categories screen documentation for the distinction between category archives and category management.

Product prices and availability are accurate as of the date/time indicated and are subject to change. Any price and availability information displayed on Amazon at the time of purchase will apply.