Archived topic
List subcategories
20 replies · Started by ETO on September 16, 2018
Hello,
I would like to list sub-categories below the description of the main category. Do you have a code for this, please?
Hi there,
I don't think there is an easy way to do this.
Have you seen an example somewhere that you can link me to?
You can also try this plugin:
https://wordpress.org/plugins/sub-categories-widget/
Let me know :)
The Newspaper theme has such a option. I would like to do this without a plugin so that it doesn't slow down my website.
Can you link me to it?
Are you using the regular category title/description, or are you using a Page Hero in the Elements module?
I am using the regular category title/description. I have just one Page Hero for front page.
So you would need to create a shortcode to grab those sub categories.
Maybe something like:
add_shortcode( 'list_subcats', function( $atts ) {
$atts = shortcode_atts(
array(
'id' => '',
), $atts, 'list_subcats' );
ob_start();
$term_id = $atts['id'];
$taxonomy_name = 'category';
$term_children = get_term_children( $term_id, $taxonomy_name );
echo '<ul class="list-subcats">';
foreach ( $term_children as $child ) {
$term = get_term_by( 'id', $child, $taxonomy_name );
echo '<li><a href="' . get_term_link( $child, $taxonomy_name ) . '">' . $term->name . '</a></li>';
}
echo '</ul>';
return ob_get_clean();
} );
Then we need to allow shortcodes in your category descriptions with a function like this:
add_filter( 'category_description', 'do_shortcode' );
Both of the above can be added using one of the methods here: https://docs.generatepress.com/article/adding-php/
Then just add the shortcode to your description: [list_subcats id="10"]
Be sure to update the 10 with the ID of the category you're in.
Thanks so much. But, there isn't just one main category. I have several main categories and each has its own sub categories. So, what I want is that each main category should display its own sub categories.
By the way, Could you please clarify the steps to add codes? Which code to functions.php/Snippets Plugin/GP elements, etc?
Yes - you'd need to add the shortcode to each category description, and update the ID in the shortcode to match the category you're displaying on.
I just added a note above about where to add the functions :)
What about the code here?
Using that method, you could try something like this:
add_shortcode( 'list_subcats', function() {
ob_start();
$current_cat = get_queried_object();
$term_id = $current_cat->term_id;
$taxonomy_name = 'category';
$term_children = get_term_children( $term_id, $taxonomy_name );
echo '<ul class="list-subcats">';
foreach ( $term_children as $child ) {
$term = get_term_by( 'id', $child, $taxonomy_name );
echo '<li><a href="' . get_term_link( $child, $taxonomy_name ) . '">' . $term->name . '</a></li>';
}
echo '</ul>';
return ob_get_clean();
} );
Thanks. I want to add the code to see if it will work. So, do I need to add the code above to my site using Snippets plugin? If, yes, shall I set it to "only run on site front-end"?
Then, I think I need to add the shortcode below using Elements.
[list_subcats]
I believe you'll need to run it everywhere.
Then yes - that's the shortcode you'd use to list the categories.
Thanks, Tom. It works great.
One more thing is that I would like to add a h2 title above the subcategory listing like this:
<h2>Subcategories of [current category title]</h2>