Wednesday, September 14, 2022
The Great Tit
Updated: 14. 9. 2022, Added: 14. 9. 2022

Display only parent categories in WordPress

If you want to list all Post Categories in WordPress without displaying the child categories, then you must use parent = 0 in the query. Here is the example which display the category links wrapped by the DIV.

<?php
$args = array(
    'taxonomy'           => 'category',
    'hide_empty'         => 0,
    'orderby'            => 'name',
    'order'              => 'ASC',
    'show_count'         => 0,
    'use_desc_for_title' => 0,
    'title_li'           => 0,
    'style'              => 'none',
    'echo'               => 0,
    'parent'             => 0,
);

$categories = wp_list_categories($args);
if ( $categories ) {
    printf( '<div class="cat-links">%s</div>', $categories );
}
?>
Did I help you? Support me on Patreon!
Beep this article to your friends!

Leave a Reply

Your email address will not be published. Required fields are marked *

This site uses Akismet to reduce spam. Learn how your comment data is processed.

linkedin facebook pinterest youtube rss twitter instagram facebook-blank rss-blank linkedin-blank pinterest youtube twitter instagram