Site logo

[Resolved] Dynamic subcategory titles and descriptions on a main category page

Home Forums Support [Resolved] Dynamic subcategory titles and descriptions on a main category page

Home Forums Support Dynamic subcategory titles and descriptions on a main category page

Viewing 6 posts - 1 through 6 (of 6 total)
  • Author
    Posts
  • #2468041
    Joeri

    Hi,

    I’ve created a main category page using Elements and a Customer Header including a title and description as below:
    ss

    This is now a ‘manual’ block. I now need to edit this block manually whenever I create new subcategories, but I want to make it a loop (?) or dynamic content (?) so the subcategory titles and descriptions get loaded automaticly. Also when new ones are created.

    Can you help me achieve this?

    #2468170
    David
    Staff
    Customer Support

    Hi there,

    its not something that can be done using Blocks, currently the GB Query Loop can only loop through posts, not post terms or other types of data.

    Heres a PHP Snippet that creates a shortcode which will output a list of child terms and its descriptions:

    function show_child_terms() {
        $tax_name = 'category';
        $current_term = get_queried_object();
        $term_id = $current_term->term_id;
        $term_children = get_terms( $tax_name, array( 'child_of' => $term_id ) );
    
        if ( empty( $term_children ) ) {
            return;
        }
        $html = '<ul class="child-terms">';
        foreach( $term_children as $term ) {
            $name = $term->name;
            $link = get_term_link( $term );
            $desc = $term->description;
            $title = ( $link ) ? '<a class="term-title" href="' . $link . '">' . $name  . '</a>' : '<span class="term-title">' . $name . '</span>';
            $description = ( $desc ) ? '<p class="term-description">' . $term->description . '</p>' : '';
            $html .= '<li>' . $title . $description . '</li>';
        }
        $html .= '</ul>';
        return $html;
    }
    add_shortcode ('child_terms', 'show_child_terms' );

    Then you can add the [child_terms] shortcode to the element.
    It will require some custom CSS to make it look like a grid, which i can help with if you need.
    Not sure about adding those icons before the titles though… as those would need to be stored somehow in the term meta.

    #2604551
    Thijs

    Hello David,

    Your code works really well for me, many thanks! I was wondering if you could limit the child terms to direct subcategories of the main category? The code shows all subcategories (inlusied their subcategories).

    For example my structure: site.com/parent/subcategory/subsubcategory/subsubsubcategory/post

    Many thanks in advance.

    #2604738
    Ying
    Staff
    Customer Support

    Hi Thijs,

    Can you be more specific about where and how you gonna use the sub-category list?

    Is it going to be on a static page or an archive page?

    Do you only need one sub-category list of a specific category?

    If it’s ok, we’d prefer a new topic and please link us to the page where you want to use the list.

    #2606180
    Thijs

    Hi Ying,

    With the help of some other topics I figured it out. Thanks any way for your reaction!

    #2606240
    Ying
    Staff
    Customer Support

    Glad to hear that 🙂

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