- This topic has 12 replies, 6 voices, and was last updated 1 year, 3 months ago by Fernando.
-
AuthorPosts
-
June 3, 2021 at 8:46 am #1808980Alessandro
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:
– HomeI 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?
June 3, 2021 at 7:08 pm #1809481ElvinStaffCustomer SupportHi Alessandro,
Tom’s code from the same topic should work –
https://generatepress.com/forums/topic/list-subcategories/#post-699132It’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.June 4, 2021 at 1:11 am #1809673AlessandroThanks 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
– ThreeIf 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
June 4, 2021 at 2:15 am #1809730AlessandroI 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?June 4, 2021 at 9:34 am #1810427TomLead DeveloperLead DeveloperHi 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(); } );
June 4, 2021 at 10:03 am #1810465AlessandroThank you so much it works perfectly!
June 5, 2021 at 8:54 pm #1811850TomLead DeveloperLead DeveloperGlad I could help ๐
January 3, 2023 at 8:13 am #2482319JacopoHi 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
January 3, 2023 at 9:39 am #2482460DavidStaffCustomer SupportSomething 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
January 3, 2023 at 10:37 pm #2482970JacopoWonderful, 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
January 3, 2023 at 11:35 pm #2483009Fernando Customer SupportHi 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.
January 4, 2023 at 12:52 am #2483046JacopoThank you very much Fernando!!
January 4, 2023 at 12:55 am #2483048Fernando Customer SupportYou’re welcome, Jacopo!
-
AuthorPosts
- You must be logged in to reply to this topic.