-
Anonymous
Hi,
I want to add a category like this page. Where it includes countries for each continent.For example for Europe, have it include countries like France, Iceland etc.. (that will go to those country category pages).
How can I do this?
-
Hi there,
It looks like your parent and child categories are already set up correctly. What you need is to automatically show a list of subcategories on the parent category archive page.
GP and GB don’t have a built-in option for this, so it will require some custom code. You can use this shortcode to output subcategories of the current parent:
function subcategory_list_shortcode($atts) { $atts = shortcode_atts(array( 'parent' => false, ), $atts); $parent_id = $atts['parent']; if (!$parent_id) { $parent_cat = get_queried_object(); $parent_id = $parent_cat->term_id; } $args = array( 'taxonomy' => 'category', 'child_of' => $parent_id, ); $subcategories = get_terms($args); if (empty($subcategories)) { return; } $output = '<ul class="subcategory-list">'; foreach ($subcategories as $subcategory) { $term_link = get_term_link($subcategory); $output .= '<li><a href="' . esc_url($term_link) . '">' . esc_html($subcategory->name) . '</a></li>'; } $output .= '</ul>'; return $output; } add_shortcode('subcategory_list', 'subcategory_list_shortcode');How to add PHP: Adding PHP – GeneratePress Docs
As your archive pages are already using a
GP Block ElementPage Hero, you can add the[subcategory_list]shortcode directly inside that Block Element. Remember to enable Execute Shortcodes in the settings.The output may need some CSS for styling, depending on how you want the subcategories to look.
-
Anonymous
thanks very much
-
You’re very welcome!
- You must be logged in to reply to this topic.