Pro - Loopers - Category Loop On Home Page - Specific Catergory Only

Hi Team,
looking to loop through “sub categories” of a specific category on the home page.

Format is:
Category Level 1
-Category Level 1.1
-Category Level 1 .2
-Category Level 1 .3

From Category 1.1, 1.2, 1.3 i want to display the title, the featured image (i have used ACF to create a custom field) and the category description.

I have been able to loop through “Type:All Terms - Taxonomy: Categories” which displays all categories but i have been unable to work out how to loop through and draw content from the sub categories of just a specific term.

I hope you can help.

Hi Team, i have been able to achieve this using the following code which i included in my child themes functions.php.

/** * Filter for home page. use 'child_of' to display full depth. use 'parent' to display only first level. * */

add_filter(‘cs_looper_custom_subcat’, function($result, $args)
{

$cat_args=get_categories(
array( ‘child_of’=>79, ‘taxonomy’=> ‘category’,‘hide_empty’=> 0 )
);

$subcats = $cat_args;
return $subcats;
}, 10, 2);

The hook is the called by a looper provider using a type of “Custom” and a hook of “subcat”

If you could advise how i could modify the code to filter to a category ID inserted in to the JSON params of the custom hook, that would be very helpful. I have had to hard code the id and for me that is far from extensible.

Hi @etheodore,

Glad that you are to find the solution. You can send the JSON parameter to the Custom Looper hook if you need that to be changed in the future. Please remember that to follow the given format to pass and fetch the parameters.

{ "parent_cat":"25"}  //JSON formatted parameter to send into the custom looper


$parent_cat=$args['parent_cat']; //this is the way you can get the parameter inside the custom looper hook.

Hope it helps.
Thanks

Hi Tristup, thanks for that. I have found that code in another thread but have been struggling for how to incorporate that in to the code. Could you please kindly provide a functional code snipped for insertion in to functions.php including

$parent_cat=$args[‘parent_cat’];

For anyone looking for the answer, the following code works:

Use the following in your parameters to pass the parent category ID.

{ "parent_cat":"25"} 

Function:

/**
 * Filter for home page. use 'child_of' to display full depth. use 'parent' to display only first level.
 *
 */


add_filter('cs_looper_custom_subcat', function($result, $args)
{
$parent_cat=$args['parent_cat'];
$cat_args=get_categories(
array( 'child_of'=>$parent_cat, 'taxonomy'=> 'category','hide_empty'=> 0 )
);

$subcats = $cat_args;
return $subcats;
}, 10, 2);
1 Like

Glad to hear you’ve tweaked the code to work for you.

Please just note that the code provided here in the forum only serves as a guide. It’s not guaranteed to work and we cannot provide support for it to make it work nor improve it. For that, you need to know how to work with code. We can just give you a little idea.

Thanks.

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