- This topic has 5 replies, 4 voices, and was last updated 2 years, 5 months ago by
Ying.
-
AuthorPosts
-
December 20, 2022 at 5:03 am #2468041
Joeri
Hi,
I’ve created a main category page using Elements and a Customer Header including a title and description as below:
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?
December 20, 2022 at 7:23 am #2468170David
StaffCustomer SupportHi 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.April 10, 2023 at 12:54 pm #2604551Thijs
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.
April 10, 2023 at 4:47 pm #2604738Ying
StaffCustomer SupportHi 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.
April 11, 2023 at 1:05 pm #2606180Thijs
Hi Ying,
With the help of some other topics I figured it out. Thanks any way for your reaction!
April 11, 2023 at 2:14 pm #2606240Ying
StaffCustomer SupportGlad to hear that 🙂
-
AuthorPosts
- You must be logged in to reply to this topic.