How to get parent and child sub category (Taxonomy) order to display

To display a list of categories with their child categories following the parent categories, you can use WordPress’s get_categories() function to fetch the categories and their child categories.

Simple script print to show parent categories with own child category attached in looping you can review the code and implement to your code as per reference.

For Example

Parent Category

 - Child Category 
 - child category

Parent Category

 - child category



$categories = get_categories(array(
    'orderby' => 'name',
    'order' => 'ASC',
    'hide_empty' => false // Include empty categories as well
));

$parent_categories = array();
$child_categories = array();

// Separate parent and child categories
foreach ($categories as $category) {
    if ($category->parent == 0) {
        $parent_categories[] = $category;
    } else {
        $child_categories[$category->parent][] = $category;
    }
}

// Display parent categories and their child categories
foreach ($parent_categories as $parent_category) {
    echo '<h2>' . $parent_category->name . '</h2>';

    if (isset($child_categories[$parent_category->term_id])) {
        echo '<ul>';
        foreach ($child_categories[$parent_category->term_id] as $child_category) {
            echo '<li>' . $child_category->name . '</li>';
        }
        echo '</ul>';
    }
}

deneme bonusudeneme bonusu veren sitelerpulibet girişOnwin Güncel Giriştürkçe altyazılı pornocanlı bahis casino