• I am wondering how to output list of children category titles under their parents category title.
    For example if Women is a parent category name and there are 3 child categories “skirt, pants and shirts” I want them to be printed
    Women

    • Skirt
    • Pants
    • Shirts

    I currently have following code

    <ul>
        <?php
        $categories = get_categories('exclude=');
    
        foreach ($categories as $cat) {
    
            echo '<li><a href="' . get_category_link($cat->cat_ID) . '>' . $cat->cat_name . '<small> (' . $cat->count . ')</small></a>';
    
            echo '<ul class="list1">';
    
            $loop = new WP_Query(array(
                'posts_per_page' => -1,
                'cat' => $cat->cat_ID
            ));
    
            while ($loop->have_posts()) {
    
                $loop->the_post();
    
                $category = get_the_category();
    
                // Only display a post link once, even if it's in multiple categories
                if ($category[0]->cat_ID == $cat->cat_ID) {
                    echo '<li><a href="' . get_permalink() . '">' . get_the_title() . '</a></li>';
                }
            }
    
            echo "</ul>";
            echo "</li>";
    
            wp_reset_postdata();
        }
        ?>
    </ul>

    But this code doesn’t display “parent and child” structure. Moreover they are all mixed up where I cannot tell which one is parents and their children.

    I would like to do the parents and children structure applies to this post category.

    Your help is very much appreciated!
    Thank you!

Viewing 3 replies - 1 through 3 (of 3 total)
  • Hello,
    You can try adding this code,

    $terms = get_categories( array(
    	  'orderby' => 'name',
    	  'parent'  => 0
             ) );
    echo '<ul>';
    foreach ($terms as $term) {
      $categories = get_categories('child_of='.intval($term->term_id));
      echo '<li><a>term_id).'">'.$term->name.'</a>';
     echo '<ul>';
      foreach ($categories as $category) {
       if ($category->parent ==$term->term_id):
       echo '<li class="subcategory"><a>term_id).'">'.$category->cat_name.'</a></li>';
       endif;
    }
    echo '</ul>';
    echo '</li>';
    }
    echo '</ul>';

    Regards,
    Sarmistha

    Thread Starter palmtree

    (@toshirobot)

    Hi Sarmistha,

    Thank you for taking a look.

    Your code is very close. I could establish the parent and children structure output but the URL of the child category is not printed. Please see my code below. It’s bit modified because your a href code did not work. Now parent category name and URL are correct. And children’s category name is correct but their URL..

    <?php
    	$terms = get_categories( array(
    	  'orderby' => 'name',
    	  'parent'  => 0
    		 ) );
    
    	echo '<ul>';
    
    		foreach ($terms as $term) {
    			
    			$categories = get_categories('child_of='.intval($term->term_id));
    			echo '<li><a href="'.get_category_link($term->term_id).' ">'.$term->name.'</a>';
    
    			echo '<ul>';
    
    			foreach ($categories as $category) {
    			if ($category->parent ==$term->term_id):
    			echo '<li><a href="'.get_category_link($term->term_id).'">'.$category->cat_name.'</a></li>';
    			endif;
    		}
    		echo '</ul>';
    		echo '</li>';
    		}
    	echo '</ul>';
    ?>
    • This reply was modified 7 years, 5 months ago by palmtree.
    Thread Starter palmtree

    (@toshirobot)

    Now sorted out. This works as I wanted.

    $terms = get_categories( array(
      'orderby' => 'name',
      'parent'  => 0,
         ) );
    echo '<ul>';
            foreach ($terms as $term) {
            $categories = get_categories('child_of='.intval($term->term_id));
            echo '<li><a href="' .get_category_link($term->term_id). '">'.$term->name.'</a>';
            echo '<ul>';
            foreach ($categories as $category) {
            if ($category->parent==$term->term_id):
            echo '<li><a href="'. get_term_link( $category ). '">'.$category->cat_name.'</a></li>';
            endif;
        }
        echo '</ul>';
        echo '</li>';
        }
    echo '</ul>';
Viewing 3 replies - 1 through 3 (of 3 total)
  • The topic ‘Output category titles in parents & children order’ is closed to new replies.