Aleš Sýkora / September 14, 2022 / 0 comments

Display only parent categories in WordPress

Post summary: 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.

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 );
}
?>

Fuel my passion for writing with a beer🍺

Your support not only makes me drunk but also greatly motivates me to continue creating content that helps. Cheers to more discoveries and shared success. 🍻

0 comments

Share Your Thoughts