So it won’t work in index or archives templates?

Hello, I began to try to work with Loops and Logic templates… and it’s said that it won’t work outside post or page template ? It’s very frustrating, as I thought it would be a nice way to add conditions to index or archive loops.

Hi Jean-Michel, where are you seeing that “it won’t work outside post or page template”?

This is a little under-documented right now other than a few examples on this forum, but it is entirely possible to use Loops & Logic on post archives. The way to do it is to use the <Loop> tag by itself, allowing you to loop through whatever the query is on the current archive page.

The one thing you can’t do is modify the underlying query on an archive page using L&L. But there are some way to get around that. One way would be to filter your loop with conditional logic using the If tag. So if you were on a post archive and wanted to filter by a certain author, you could write something like:

<Loop>
  <If field=author_id is value=123>
    <Field title />
  </If>
</Loop>

If you don’t want to just use the default loop of the archive and also have needs that are too complex to simply use some conditional logic, another option if you need to modify the archive page’s query using L&L is to recreate it. There are three archive fields that can be useful for this, archive_author , archive_term , and archive_post_type. If you wanted to create a author archive template in L&L, you might do something like what I’ve written below. The first loop allows you to get the data about the author of the current author archive (such as their ID) and then the second loop uses that author ID to define a query within L&L. You could also do this as a nested loop but I split it into two parts so that it was easier to see what it’s doing.

<Loop field=archive_author>
  <Set current_author_id><Field id /></Set>
</Loop>

<Loop type=post author="{Get current_author_id}">
  <Field title />
</Loop>

Hopefully, that gives you some options when working with Loops & Logic on archive pages. I’ll be sure to add some examples and documentation to clarify this.

Sorry, I’m French so my english may be very bad.

When I add a L&L block in index template of Twenty Twenty Two FSE theme, I cannot edit the field - it’s impossible. It will work in post template.

Oh gotcha, no worries, I just didn’t have much to go on from your initial post. Right, I hadn’t played around much with FSE but it does seem like putting the Tangible Template block on archive/index pages seems not to work as you’d expect. I’ll let the devs know to look into this.

2 Likes

OK, thanks. I hope there will be soon a fix for this.

Last update seems to fix the problem. Coooool. Thanks to the developers.

2 Likes

There’s still more work to do, FSE actually has a lot of bugs we have to work around since it’s in beta right now, but we’re finding workarounds and will keep publishing updates to improve the integration!

1 Like