Category Immediate Children Looper

Can someone direct me in the direction of showing “Current Category Children” of a Term?

Let’s say I have a main category called “Hire” and under Hire, I have “Hire Cat 1”, “Hire Cat 2”, “Hire Cat 3” and etc. Now when I’m on the Hire Archive page I would like underneath the Archive Title to show the children of this category and only the immediate children. Similar to the Screenshot.

I’ve tried using the current post-term but it only shows one category for some reason and it’s not even the children category. I’ve tried doing the suggestion that @tristup mentioned on this forum Reading elements of a looper, I can get all the children posts showing but not the immediate children which is what I’m looking for.

Can I get some help here please? Feel like this should really be an option available similar to Current Post Terms option because this is a very commonly used layout for ecommerce websites.

Hi @Maratopia_Digital,

Thanks for reaching out.
I would suggest you go through the following thread where the Sub Category is shown based on the parent category.


Hope it helps.
Thanks

Thanks for sending these over, I’ve been now able to get these subcategories. I still have a little problem though. It’s getting all the sub-subcategories in the sub-categories which is not what I’m looking for. So here is the structure that I’m hoping to achieve.

Category 1 (Hire)
   Category 1.1 (Press Fitting)
      Category 1.1.1 (Geberit)
      Category 1.1.2 (Pegler Yorkshire)
      Category 1.1.3 (Sanha)
   Category 1.2 (Pipe Fitting)
      Category 1.2.1 (Pipe Threading Machines)
      Category 1.2.2 (Hand Held Threading Machines)
      Category 1.2.3 (Pipe Stillsons & Chain Wrenches)
   Category 1.3 (Fusion Welding)
      Category 1.3.1 (Polypipe Terrain Fuze HDPE)
      Category 1.3.2 (Geberit)
      Category 1.3.3 (Vulcathene)

So on the archive page for HIRE I will only want to show the immediate Category (Press Fitting, Pipe Fitting, Fusion Welding) and not show all of the categories under Hire which would then include all the sub-subcategories. And then subsequently on the archive page for Category 1.1, I would only want category 1.1.1.

Hopefully, this makes a bit of sense. I’ll set a secure note if you want to have a closer look.

Hi @Maratopia_Digital,

To get only direct child categories, please try by using the parent instead of child_of. I would suggest you go through the following article on this.


Hope it helps.
Thanks

Awesome thanks for this advice, I’ve been able to get it working. For any looking for this solution, this one has worked for me:

add_filter('cs_looper_custom_subcat', function($result, $args)
{
$cat_args=get_categories(
array( 
	'taxonomy'=> 'product_cat',
	'hide_empty'=> 1,
	'parent'=>get_queried_object_id(),
)
);

return $cat_args;
}, 10, 2);

I was trying to use the JSON to input the parent id with a {{dc:archive:id}} like mentioned on the previous example @tristup gave but there seems to be a bug on the JSON editor that it’s not pushing the dynamic content through to the function. So I had to find a workaround and was able to find this one with the function get_queried_object_id().

Thanks, @tristup for the speedy response time.

Hi @Maratopia_Digital,

Glad that we are able to help you.

Thanks

This topic was automatically closed 10 days after the last reply. New replies are no longer allowed.