[Solved] Looper for listing only child terms

A bit of a similar question to the one I had before about nested loops, but instead of listing only the child terms of the current parent category being accessed, I want to list all child terms.

It’s easy enough to use the All Terms option when using a Looper Provider but that will list all parents and child terms. The brute force method would be to exclude the parent terms in the Taxonomy option but this becomes a bit of an ugly solution if you have 10, 20 parent terms involved.

I’ve been thinking through some solutions and it basically boils down to:

  • Use Looper Provider (All Terms) and exclude parent terms
  • Make a menu widget with only children terms
  • Custom filter code

I’m going to try to build something custom based off the help I’ve received in the past as that seems like the best option but if I could, I would love to suggest a future feature for using the All Terms Looper Provider option where excluding all parent terms would be possible. :grinning:

Trying to think my way through the 3rd option here but I know that if I were to use custom code, i can do the reverse and get all the top level terms at least:

$parent_terms = get_terms([
	'taxonomy'   => 'partner',
	'hide_empty' => 'true,',
	'parent'	 => 0
]);

// Handle potential errors
if (is_wp_error($parent_terms)) {
    return [];
}

return $parent_terms;

Only question is figuring out where to go from here to extract just the children terms.

After some spelunking, I found a solution.

	$parent_terms = get_terms([
	'taxonomy'   => 'partner',
	'childless'  => true
]);

Since this returns terms that have no children, parent terms are excluded and I am able to list all child terms in the looper without issue. Of course, should the scope of what I’m trying to do change and I have to deal with more than 1 level of children then this code may not work so a more robust solution may be necessary in the future.

Hello @dobacco,

Thanks for writing to us.

Glad that you were able to list the subcategory. For your reference, you can check these threads as well.


Please note that the code provided in the above thread serves as a guide only and is to help you in getting started so implementing it and maintaining the code will be out of our support scope and for further maintenance for new possible versions of the theme that may cause the customization to break, you will need to hire a developer or subscribe ti One where customizations questions are answered.

Thanks for understanding

Prakash,

Thanks for the links but I did look at those earlier as also when I was asking questions about nested loops a while ago. Nothing wrong with the suggestions but those expect an input (parent category) when I need to output an entire list of child terms. In the end, I plan to put both methods to work so I appreciate the response anyway.

Good to hear that @dobacco. Cheers.