Sub category shortcode
-
Hi is a it possible to add sub categories to the single categories page using a shortcode, to filter further
-
I’ve added a shortcode but it’s pulling the listings up in those categories not the subcategories, even if i list them or the id
By default, Directorist does not support displaying subcategories on a single category page via shortcode in the way you described. The standard shortcodes will pull listings from the selected categories, not dynamically list child subcategories.
That said, you can achieve this behavior with a bit of custom code. The snippet below creates a custom shortcode that will first display the child categories of the current category and then show the listings from the parent category.
You can add the code below to your theme’s
functions.phpfile or use a code snippet plugin to apply it safely:/**
* Shortcode: [directorist_advanced_category ...any directorist_category attrs...]
*
* Works on a Directorist category archive where the current term slug
* is available via get_query_var('atbdp_category').
* Shows child categories first, then parent category listings.
*/
add_shortcode('directorist_advanced_category', function ($atts = [], $content = null) {
$orig_atts = is_array($atts) ? $atts : [];
// Helper to build safe shortcode attribute string
$build_attr_str = function(array $a) {
$pairs = [];
foreach ($a as $k => $v) {
if ($v === '' || $v === null) continue;
$k = sanitize_key($k);
if (is_array($v)) { $v = implode(',', array_map('sanitize_text_field', $v)); }
$pairs[] = sprintf('%s="%s"', $k, esc_attr($v));
}
return implode(' ', $pairs);
};
// Get current category slug from query var
$current_slug = get_query_var('atbdp_category');
if (!$current_slug) {
return do_shortcode('[directorist_category ' . $build_attr_str($orig_atts) . ']');
}
$parent = get_term_by('slug', $current_slug, ATBDP_CATEGORY);
if (!$parent || is_wp_error($parent)) {
return do_shortcode('[directorist_category ' . $build_attr_str($orig_atts) . ']');
}
$output = '';
// Gather child categories first
$child_terms = get_terms([
'taxonomy' => ATBDP_CATEGORY,
'parent' => (int) $parent->term_id,
'hide_empty' => false,
]);
if (!is_wp_error($child_terms) && !empty($child_terms)) {
$child_slugs = [];
foreach ($child_terms as $child) {
$child_slugs[] = $child->slug;
}
$child_slugs = array_values(array_unique($child_slugs));
if (!empty($child_slugs)) {
$all_cats_attr = 'slug="' . esc_attr(implode(',', $child_slugs)) . '"';
$output .= do_shortcode('[directorist_all_categories view="grid" ' . $all_cats_attr . ']');
}
}
// Then display all parent category listings
$output .= do_shortcode('[directorist_category slug="' . esc_attr($parent->slug) . '" ' . $build_attr_str($orig_atts) . ']');
return $output;
});
add_filter('atbdp_all_categories_argument', function($args){
if(get_directorist_option('single_category_page')) unset($args['parent']);
return $args;
});After adding this, you can use the shortcode below on your category page:
[directorist_advanced_category]Please give this a try and let me know how it works on your end.
Best regards,
Al-Amin Khangreat thanks, I’ll have a play and let you know how I get on 🙂
You must be logged in to reply to this topic.