Author is Aleš Sýkora
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 );
}
?>