- This topic has 20 replies, 2 voices, and was last updated 1 year, 4 months ago by
Leo.
-
AuthorPosts
-
September 16, 2018 at 5:19 am #678420
ETO
Hello,
I would like to list sub-categories below the description of the main category. Do you have a code for this, please?GeneratePress 2.1.4GP Premium 1.7.2September 16, 2018 at 9:40 am #678629Leo
StaffCustomer SupportHi 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 ๐
Documentation: http://docs.generatepress.com/
Adding CSS: http://docs.generatepress.com/article/adding-css/September 16, 2018 at 11:10 am #678678ETO
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.
September 16, 2018 at 11:13 am #678679Leo
StaffCustomer SupportCan you link me to it?
Documentation: http://docs.generatepress.com/
Adding CSS: http://docs.generatepress.com/article/adding-css/September 16, 2018 at 12:01 pm #678698September 16, 2018 at 6:33 pm #678829Tom
Lead DeveloperLead DeveloperAre you using the regular category title/description, or are you using a Page Hero in the Elements module?
Documentation: http://docs.generatepress.com/
Adding CSS: http://docs.generatepress.com/article/adding-css/
Ongoing Development: https://generatepress.com/ongoing-developmentSeptember 17, 2018 at 12:58 am #678952ETO
I am using the regular category title/description. I have just one Page Hero for front page.
September 17, 2018 at 11:09 am #679474Tom
Lead DeveloperLead DeveloperSo 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.Documentation: http://docs.generatepress.com/
Adding CSS: http://docs.generatepress.com/article/adding-css/
Ongoing Development: https://generatepress.com/ongoing-developmentSeptember 17, 2018 at 12:36 pm #679551ETO
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?
September 17, 2018 at 8:26 pm #679757Tom
Lead DeveloperLead DeveloperYes – 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 ๐
Documentation: http://docs.generatepress.com/
Adding CSS: http://docs.generatepress.com/article/adding-css/
Ongoing Development: https://generatepress.com/ongoing-developmentOctober 11, 2018 at 1:13 pm #699011ETO
What about the code here?
October 11, 2018 at 7:16 pm #699132Tom
Lead DeveloperLead DeveloperUsing 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(); } );
Documentation: http://docs.generatepress.com/
Adding CSS: http://docs.generatepress.com/article/adding-css/
Ongoing Development: https://generatepress.com/ongoing-developmentOctober 12, 2018 at 1:03 pm #699728ETO
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]
October 12, 2018 at 5:28 pm #699815Tom
Lead DeveloperLead DeveloperI believe you’ll need to run it everywhere.
Then yes – that’s the shortcode you’d use to list the categories.
Documentation: http://docs.generatepress.com/
Adding CSS: http://docs.generatepress.com/article/adding-css/
Ongoing Development: https://generatepress.com/ongoing-developmentOctober 13, 2018 at 2:09 am #699963ETO
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>
-
AuthorPosts
- You must be logged in to reply to this topic.