WordPress.org

Ready to get started?Download WordPress

Forums

List of sub categories to create a catalog (2 posts)

  1. didiair
    Member
    Posted 1 year ago #

    I am wondering around to find a solution how to list sub categories as if the were like posts under parent category. This is what I'm looking for..??

    http://www.otoritcom.com/wp-content/uploads/2013/03/subcatList.jpg

  2. Simalam
    Member
    Posted 1 year ago #

    Hello didiair,

    You could get all the parent categories, then loop through them getting the children and displaying them how you want.

    //first get the category
    $args = array(
        'orderby'       => 'name',
        'order'         => 'ASC',
        'parent'         => 0
    );
    
    $terms = get_terms( 'category_taxonomy_name', $args ); //put category taxonomy name here
    
    //loop through categories
    if($terms){
        foreach($terms as $term){
            echo '<h2>' . $term->name . '</h2>';
            $term_children = get_term_children( $term, 'category_taxonomy_name' ); //place your category taxonomy name here
            if($term_children){
                foreach($term_children as $child){
                    echo '<h3>' . $child->name . '</h3>';
                    echo '<p>' . $child->description . '</p>';
                    echo '<a href="'.get_term_link($child->slug, 'category_taxonomy_name').'">'.$term->name.'</a>'; //place your category taxonomy name here
                }
            }//end children
    
        }//end categories
    }//end if categories

Topic Closed

This topic has been closed to new replies.

About this Topic