Archived topic
Show list sub category in category page
12 replies · Started by Alessandro on June 3, 2021
Hi guys,
in the various Wordpress categories I would like to show a list of the subcategories located within the category.
Ex.
if I am in the category:
- Home
I would like a list of sub-categories to be shown in the template archive page eg:
- Kitchen
- Bathroom
- Bedroom
....
How could I do?
there is a tread here: https://generatepress.com/forums/topic/list-subcategories/ but it is recommended to open a new topic.
Finally, to call the script and insert the list of categories after all the articles listed in the category, should I use a Hook?
Hi Alessandro,
Tom's code from the same topic should work -
https://generatepress.com/forums/topic/list-subcategories/#post-699132
It's a PHP code, here's how to add PHP - https://docs.generatepress.com/article/adding-php/
It's either go for code snippets plugin or install a child theme for its functions.php. :)
Tom's code allows you to use the shortcode [list_subcats]. You can use this anywhere in the theme that allows for shortcodes. And yes, you can use hooks if you must depending on where you want to place this in.
Thanks it works perfectly!
Is it possible to show only the main categories?
es of tree:
- One
- Two
- - Two point one
- - - Two point one point one
- - Two point two
- - Two point three
- Three
If I am in the "two" category, at the moment I see both "Two point one" and "Two point one point one" (his son)
is it possible to make sure to show only "Two point one" without his sons?
Thanks
I noticed that if the category is empty, it still shows in the list.
Is it possible to make sure that if empty it is not shown?
Hi there,
This might be helpful: https://medium.com/@stefanledin/wordpress-how-to-get-only-direct-child-categories-286ad724f04b
add_shortcode( 'list_subcats', function() {
ob_start();
$current_cat = get_queried_object();
$term_id = $current_cat->term_id;
$categories = get_categories( array(
'parent' => $term_id,
'hide_empty' => true,
) );
echo '<ul class="list-subcats">';
foreach ( $categories as $category ) {
echo '<li><a href="' . esc_url( get_category_link( $category->term_id ) ) . '">' . $category->name . '</a></li>';
}
echo '</ul>';
return ob_get_clean();
} );
Thank you so much it works perfectly!
Glad I could help :)
Hi guys! You are always great with support!
I wanted to ask, how does this code change if I use custom taxonomies called "ambito" (ambitos plural) instead of the standard wp categories?
Thanks
Something like this:
add_shortcode( 'list_subcats', function() {
ob_start();
$current_cat = get_queried_object();
$term_id = $current_cat->term_id;
$taxonomy_name = 'category'; // Set your taxonomy here
$term_children = get_term_children( $term_id, $taxonomy_name );
if (!empty($term_children)) {
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();
} );
set your taxonomy here: $taxonomy_name = 'category'; // Set your taxonomy here
Wonderful, thanks! Now when I am inside a parent I can see child categories perfectly!
One last thing: when I land in a child category, is it possible to see the other child categories and have a reference to the parent category? Eg.
Arts and Humanities (parent):
- Music and Art
- History
- Philosophy
When I click on History, how can I show Music and Art and Philosophy categories? How can I show a reference to the Arts and Humanities (parent) category?
Thank you very much for your help
Hi Jacopo,
You can try adding this Shortcode code:
add_shortcode( 'list_parent_and_children', function() {
ob_start();
$current_cat = get_queried_object();
$term_id = $current_cat->term_id;
$taxonomy_name = 'category'; // Set your taxonomy here
$parent = get_ancestors( $term_id, $taxonomy_name );
if (!empty($parent)){
echo '<a href="' . esc_url( get_term_link( $parent[0], $taxonomy_name ) ) . '">Parent: ' . get_term( $parent[0] )->name . '</a>';
}
$term_children = get_term_children( $parent[0], $taxonomy_name );
if (!empty($term_children)) {
echo '<ul class="list-subcats">';
foreach ( $term_children as $child ) {
if( $child !== $term_id) {
$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();
} );
Replace category in the code with your custom taxonomy slug.
You should now have this shortcode [list_parent_and_children] that you can use.
It should output the parent and the term's siblings.
Thank you very much Fernando!!
You're welcome, Jacopo!