List all subcategories from category olatechproFebruary 22, 20230 Views How can I get all subcategories from a certain category? Post Views: 0 Related postsGet rid of this Strict Standards warningWooCommerce: get_current_screen not working with multi languageCan’t login on WordPressForce HTTPS using .htaccess – stuck in redirect loopWordPress: Ajax not working to insert, query and result dataHow Can I pass an image file to wp_handle_upload?
Yes, you can use get_categories() using 'child_of' attribute. For example all sub categories of category with the ID of 17: $args = array('child_of' => 17); $categories = get_categories( $args ); foreach($categories as $category) { echo '<p>Category: <a href="' . get_category_link( $category->term_id ) . '" title="' . sprintf( __( "View all posts in %s" ), $category->name ) . '" ' . '>' . $category->name.'</a> </p> '; echo '<p> Description:'. $category->description . '</p>'; echo '<p> Post Count: '. $category->count . '</p>'; } This will get all categories that are descendants (i.e. children & grandchildren). If you want to display only categories that are direct descendants (i.e. children only) you can use 'parent' attribute. $args = array('parent' => 17); $categories = get_categories( $args ); foreach($categories as $category) { echo '<p>Category: <a href="' . get_category_link( $category->term_id ) . '" title="' . sprintf( __( "View all posts in %s" ), $category->name ) . '" ' . '>' . $category->name.'</a> </p> '; echo '<p> Description:'. $category->description . '</p>'; echo '<p> Post Count: '. $category->count . '</p>'; } Log in to Reply
For custom post types “categories” use get_terms(). (Altering @Bainternet’s answer) $categories = get_terms( array( 'taxonomy' => 'product_cat', 'hide_empty' => false, 'parent' => 17 // or //'child_of' => 17 // to target not only direct children ) ); foreach($categories as $category) { echo '<p>Category: <a href="' . get_category_link( $category->term_id ) . '" title="' . sprintf( __( "View all posts in %s" ), $category->name ) . '" ' . '>' . $category->name.'</a> </p> '; echo '<p> Description:'. $category->description . '</p>'; echo '<p> Post Count: '. $category->count . '</p>'; } Log in to Reply
Yes, you can use get_categories() using
'child_of'
attribute.For example all sub categories of category with the ID of 17:
This will get all categories that are descendants (i.e. children & grandchildren).
If you want to display only categories that are direct descendants (i.e. children only) you can use
'parent'
attribute.For custom post types “categories” use get_terms().
(Altering @Bainternet’s answer)