[Resolved] List subcategories

Home Forums Support [Resolved] List subcategories

Home Forums Support List subcategories

Viewing 15 posts - 1 through 15 (of 21 total)
  • Author
    Posts
  • #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.4
    GP Premium 1.7.2
    #678629
    Leo
    Staff
    Customer Support

    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 ๐Ÿ™‚

    #678678
    ETO

    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.

    #678679
    Leo
    Staff
    Customer Support

    Can you link me to it?

    #678698
    ETO

    It was something like here.
    And the documentation is here.

    Screenshot

    #678829
    Tom
    Lead Developer
    Lead Developer

    Are you using the regular category title/description, or are you using a Page Hero in the Elements module?

    #678952
    ETO

    I am using the regular category title/description. I have just one Page Hero for front page.

    #679474
    Tom
    Lead Developer
    Lead Developer

    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.

    #679551
    ETO

    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?

    #679757
    Tom
    Lead Developer
    Lead Developer

    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 ๐Ÿ™‚

    #699011
    ETO

    What about the code here?

    #699132
    Tom
    Lead Developer
    Lead Developer

    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();
    } );
    #699728
    ETO

    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]

    #699815
    Tom
    Lead Developer
    Lead Developer

    I believe you’ll need to run it everywhere.

    Then yes – that’s the shortcode you’d use to list the categories.

    #699963
    ETO

    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>

Viewing 15 posts - 1 through 15 (of 21 total)
  • You must be logged in to reply to this topic.