Looping through content types

I’m kind of struggling with this conceptually… I want to loop through content types and:

  • see if there are any items in that CT that have a specific tag
  • get and output the display name of the CT
  • loop through the matching items in that CT, displaying some details about the item (at minimum, title)

Here’s what I have so far:

<Set name=currentslug><Route part="-1" /></Set>
<Loop items=company,product,periodical,article,book,document,usergroup,event,post>
  <If loop exists type="{Field}" tag="{Get name=currentslug}">
    <h3><Format case=pascal><Field /></Format></h3>
        <li><a href="{Field url}"><Field title /></a></li>

The items variable holds the CT names I’m looping through.


Thank you :slight_smile:

That makes sense to me. I noticed your template was all in one big block and was really hard to read, so I edited your post to format it in a way that makes it easier to read. When I did that, I noticed that you seemed to be missing a closing </If> tag, so I added that. Was that the issue you were having? Or were you just checking whether this was a sensible way to approach your problem?

The only way I could think to modify your template would be using the type loop type. So instead of <Loop items=company,product,periodical,article,book,document,usergroup,event,post> you could write <Loop type=type>. And then instead of <Field /> you’d use <Field name /> or <Field label />. I haven’t played around with type loops very much and one potential downside I see is that if you have post types that use tags but that you don’t want to display, this solution wouldn’t work since it would lloop through all your post types, not just the ones you specified in your list. Using a type loop might also be a tiny bit less efficient because you’d be looping through all your post types instead of just the specific ones you’ve specified. But that tradeoff might be worth it if you plan to add additional post types in the future and you don’t want to need to update your template when you do that.

But that’s just another way to think about it. I think the template you shared looks reasonable and should work now that I’ve added that closing </If>. Let me know if you’re running into any other issues or have more specific questions!

Thank you, Ben. It’s a bit of a kludge, but I ended up using Switch with a variable holding the current content type to output the header I need.

1 Like