Display all categories including sub categories

I am trying to display a list of all categories and sub-categories underneath that category, I have managed to get a list of all categories that are parents but am struggling to detect if it has children and then display them in the current loop. Here is my code at the moment:

$args = array(
  'orderby' => 'name',
  'parent' => 0
  );
$categories = get_categories( $args );
foreach ( $categories as $category ) {

    echo '<a href="' . get_category_link( $category->term_id ) . '">' . $category->name . '</a><br />';

}

I have found a few functions on here about detecting if the category has children, but am struggling to actually display the children (up to one level) under this link.

Read More

Any help is greatly appreciated,
Simon

Related posts

5 comments

  1. I have very 4 lines solutions, It shows category and sub-categories and their posts count.

    $cats = get_terms('category');
    foreach ($cats as $cat) {
      echo $cat->name . "<br>" . "(" . $cat->count . ")"; 
    }
    
  2. Solved with the following:

    $args = array(
      'orderby' => 'name',
      'parent' => 0
      );
    $categories = get_categories( $args );
    $first = true;
      foreach ($categories as $category) {
        if ( $first )
        {
            echo '<li class="title" style="border-top: 0px;"><a href="acatalog/abovegroundpools.html">'.$category->cat_name.'</a>';
            $first = false;
        }
        else
        {
            echo '<li class="title"><a href="acatalog/abovegroundpools.html">'.$category->cat_name.'</a>';
        }
        $theid = $category->term_id;
        $children = $wpdb->get_results( "SELECT term_id FROM $wpdb->term_taxonomy WHERE parent=$theid" );
            $no_children = count($children);
        if ($no_children > 0) {
            echo "<ul>";
            $args2 = array(
             'orderby' => 'name',
             'parent' => 2
             );
            $args2["parent"]=$category->term_id;
            $categories2 = get_categories( $args2 );
             foreach ($categories2 as $category2) {
    
                echo '<li><a href="acatalog/Inflatable-Hot-Tubs.html">'.$category2->cat_name.'</a></li>';
    
            }
            echo '</ul>';
        } else {
        echo '</li>';
        }
    
      }
     ?>
    
  3. function my_Categ_tree($catId, $depth){
        $depth .= '-';  $output ='';
        $args = 'hierarchical=1&taxonomy=category&hide_empty=0&parent=';    $categories = get_categories($args . $catId);
        if(count($categories)>0){
            foreach ($categories as $category) {
                $selected = ($cat->term_id=="22") ? " selected": "";
                $output .=  '<option value="'.$category->cat_ID.'" '.$selected .'>'.$depth.$category->cat_name.'</option>';
                $output .=  my_Categ_tree($category->cat_ID,$depth);
            }
        }
        return $output;
    }
    
    echo my_Categ_tree(0,'');
    
  4. <?php
    
        // Get top level categories and list them
        $args = array('orderby' => 'name', 'parent' => 0);
        $categories = get_categories( $args );
    
        foreach ($categories as $category) {
    
        echo $category->cat_name . '<br>';
    
        $args2 = array('orderby' => 'name', 'parent' => $category->cat_ID);
        $subcategories = get_categories( $args2 );
    
        echo '<ul>';
    
        // While listing top level categories
        // list their child categories
        foreach ($subcategories as $subcategory) {
            echo '<li>' . $subcategory->cat_name . '</li>';
        }
    
        echo '</ul>';
    
        }
    ?>
    
  5. If you want to display all your categories and subcategories of custom taxonomy, use this code and make sure to provide the slug of your taxonomy.

    function ow_categories_with_subcategories( $taxonomy ) {
    
        // Get the top categories that belong to the provided taxonomy (the ones without parent)
        $categories = get_terms( 
            array(
                'taxonomy'   => $taxonomy,
                'parent'     => 0, // <-- No Parent
                'orderby'    => 'term_id',
                'hide_empty' => true // <!-- change to false to also display empty ones
            )
        );
        ?>
        <div>
            <?php
            // Iterate through all categories to display each individual category
            foreach ( $categories as $category ) {
    
                $cat_name = $category->name;
                $cat_id   = $category->term_id;
                $cat_slug = $category->slug;
    
                // Display the name of each individual category
                echo '<h3>Category: ' . $cat_name . ' - ID: ' . $cat_id . ' - Slug: ' . $cat_slug  . '</h3>'; 
    
    
                // Get all the subcategories that belong to the current category
                $subcategories = get_terms(
                    array(
                        'taxonomy'   => $taxonomy,
                        'parent'     => $cat_id, // <-- The parent is the current category
                        'orderby'    => 'term_id',
                        'hide_empty' => true
                    )
                );
                ?>
                <div>
                    <?php
                    // Iterate through all subcategories to display each individual subcategory
                    foreach ( $subcategories as $subcategory ) {
    
                        $subcat_name = $subcategory->name;
                        $subcat_id   = $subcategory->term_id;
                        $subcat_slug = $subcategory->slug;
    
                        // Display the name of each individual subcategory with ID and Slug
                        echo '<h4>Subcategory: ' . $subcat_name . ' - ID: ' . $subcat_id . ' - Slug: ' . $subcat_slug  . '</h4>';
                    }
                    ?>
                </div>
                <?php
            }
            ?>
        </div>
        <?php
    }
    ow_categories_with_subcategories( 'the_name_of_your_taxonomy' );
    

Comments are closed.