Single out children in a taxonomy loop?

I’m running this loop Loop type=taxonomy_term taxonomy=development post=“current”

to pull an image field I’ve added to my taxonomies. The developments are sorted by towns first (parent). then by subdivisions (child). I want to be able to single out fields from the child or the parent when needed, but it pulls the fields from both.

Any ideas on how to achieve this?

Hi,

Maybe related to your request, i’m looking to display only child categories of a current post, not the parent category.

The L&L template below doesn’t output the parent cat alright, but it outputs ALL child categories although only one is assigned to the post.
post=current has no effect here.

<Taxonomy category>
  <Loop field=children post=current>
    <Field title />
  </Loop>
</Taxonomy><br>

This is working, and outputs only the child category(ies).
It seems field=parent makes a difference in the loop, i don’t really understand how… :sweat_smile:

<Loop type=taxonomy_term taxonomy=category field=parent post=current>
  <Field title />
</Loop>

@dstump I think I have a partial answer, which is that you’ll probably want to use the parents query parameter which makes the loop only include top-level parent terms. So to get your town image, I assume your markup would look something like this:

<Loop type=taxonomy_term taxonomy=development post=current parents=true>
  <img src="{Field image_of_this_town}" width="500" height="600"/>
</Loop>

Unfortunately, I don’t think there’s an equivalent children=true query parameter, so the way I would show only fields from a child term would be with a nested loop:

<Loop type=taxonomy_term taxonomy=development post=current parents=true>
  <Loop type=taxonomy_term taxonomy=development post=current parent="{Field id}">
    <img src="{Field image_of_this_subdivision}" width="500" height="600"/>
  </Loop>
</Loop>

Here, the outer loop is grabbing the parent taxonomy term that’s applied to the current post and then the inner loop is grabbing content from any child of that parent term. The downside to that is that this only works if your post has both the parent and child terms applied (so you’ve checked the box for a post’s town and its subdivision). This might actually work in your case depending on how you do your data entry.

I feel like the better way to do this would be if there was a way to only loop through child terms without needing nested loops. Maybe I’m just going about this the wrong way, or maybe the devs could add a children=true query parameter in a future update which seems like it’d make the most sense. I would have thought that parents=false would do exactly that, but that didn’t seem to work in my testing.

Anyway, hope that’s helpful!

Hi @dstump My loop above outputs only the child categories alright.
I hope it’s not by accident though :sweat_smile:

@avanti I love it when things that aren’t documented just work the way you expect them to even when you aren’t sure how or why hahaha! What inspired you to try using that field query parameter on a taxonomy term loop? I’m not seeing anything in the documentation about that so I’m wondering if it’s a fluke or a feature :laughing:

Ah ah, it’s pure coïncidence, i was about to get crazy trying things all the ways when this field=parent helped… i agree, it makes no sense.

Moreover, i think field=children in this loop produces the opposite and outputs only the parent categories.

I better change my code for yours to output only the child terms, it will probably be more solid.

Speaking of the documentation, i take advantage of this example to repeat what a shame it is not to have much more example and use cases as there is in CCS doc!
It was a huge help in CCS, unfortunately L&L is quite the opposite, a rich theorical doc with long keywords lists, and a very few usage examples.

1 Like

Hi Everyone,

In the newest plugin version, the taxonomy term loop now supports children=true. (Thanks for the suggestion, Ben!) It’s basically the opposite of parents=true, which gets only the top-level terms.

A list of top-level parent terms belonging to the current post:

<Taxonomy development parents=true>
  ...
</Taxonomy>

A list of (non top-level) child terms:

<Taxonomy development children=true>

This one is strange though, I’m not sure in what situation it can be useful.

I think what’s more natural is to get the children of each top-level term, like:

<Taxonomy development parents=true>
  Town: <Term name /><br>
  Subdivisions:<br>
  <Loop field=children>
    <img src="{Term image_of_this_subdivision}" width="500" height="600"/>
  </Loop>
</Loop>

That should work, but I’m not 100% sure about the child term field - it might be necessary to replace Term with Field.


Thank you, Emmanuel, for helping in various discussion topics. I’ve been too busy to respond to all the posted questions in a timely manner, so your input is much appreciated.

Your example using field=parent is an interesting solution. It works because the loop gets all taxonomy terms belonging to the current post, then filters for only the terms which have a field called “parent”. For top-level parent terms, that field is empty.

1 Like