Loop template for all values in the list

Hi,

making templates is so tempting. :slight_smile:

I have companies directory and every company can set up their own county of operations. They can set up just one county, from the pulldown list.

I am curious how to make an overview of all companies in all counties in one page.
Like this:

County 1
- company1 (county1)
- company1 (county1)
County 2
- company 5 (county2)
- company 7 (county2)

…etc

Pseudocode:

set countylist = [county_1, .... county_n];
Loop for each item in countylist
<Loop type=profile field=county field_compare=in field_value="{first_value_from_countylist}>
<Field title />

I have learnt the Loop and Field already but I can’t figure out how to make a list and loop for every item in it.

I have done it manually now, as I duplicated Loop for every county but if there is a way to make universal loop, I will be happy :slight_smile:

Any advice warmly appreciated. (I went through documentation and it is probably there, but the solution is somehow hidden for me.)

Cheers, J

1 Like

100% YES to that hahaha! It’s a crazy powerful tool. That’s why I’m on this forum because it’s kinda fun to build templates!

I assume you’ve used ACF to make your county dropdown selector? If there was a way to loop through all the possible checkbox choices, I feel like that would make this slightly easier. Unfortunately at the moment, the ACF Select loop only allows you to loop through the items that are selected on a particular post, not all the possible choices.

Still, I think you should definitely be able to use L&L to make a list that generates the content you want automatically. You’ll probably want to look into the List tag. You could use that to create your list and then use a list loop to loop through all the counties in your list. You could then put the profile loop that you wrote inside the list loop so that for each item in the county list, you loop through all the profiles that have that list item in their county field.

Best of luck with this, let me know if you get stuck!

Hi @benjamin

Awesome, thanks for clarification.

I got to the point that I have loops but I can’t figure out how to call its items as parameter in second loop.

I have tried to compare with {Get item}, {Get Item}, {item}, item, Item … but the result is still empty.

<List name=counties>
  <Item>A</Item>
  <Item>B</Item>
  <Item>C</Item>
</List>

<Loop list=counties> <!-- first loop : go through all items in list -->
<h2><Field /></h2> <!-- display name of county -->
<!-- here we want to display all profiles that are in certain county from the list -->
<Loop type=profile field=county field_compare=in field_value=item>
<!-- field_value=item tries to compare value with the same county dropdown selector in ACF -->
    <div><Field title /></div>
    </Loop>
</Loop>

Thanks a million,
J

Hey Josef! Whenever you want to refer to a dynamic value in an attribute or parameter (as you’re trying to do here in the value of your field_value parameter), you always need to use the syntax with the curly braces { }. In this case, you’ve managed to display the county from your loop using <Field /> and then you want to refer to that same county again as a parameter value, so you’d use the same tag again, but with the syntax mentioned above like {Field}. The fact that you’re trying things like {Get item} suggests that you might be confused about the difference between the Field tag and variables, so you might want to read up on those. Your markup indentation and questions about passing data between loops make me wonder if you’d benefit from getting a better understanding about how L&L gets its loop context, so I spent a bit of time (maybe too much haha) writing up this wall of text for you if you want to deepen your knowledge of L&L. I’ve been meaning to write up a blog post anyway to illustrate that since a lot of people seem to get confused about it, so I figured I’d take advantage of your questioning to explain that here and I’ll eventually turn this comment into a blog post.

Loop context

One of the most important things to wrap your mind around when working with L&L is the context of your tags. It’s a pretty simple concept, but it’s worth spending some time on the topic because it’s so important when writing L&L markup that does what you want it to do.

All of the dynamic tags in L&L (like Loop and Field) understand the context in which they’re placed. That’s why placing <Field title /> directly into a template on a blog post shows the title of the post, whereas placing that same tag inside a post loop (like <Loop type=post><Field title /><Loop>) shows the title of each of the posts in the loop. In the first case, you can imagine that the <Field title /> tag looks around, sees that it’s been placed inside a Tangible Template block which is itself inside a blog post, and understands that the title field it should display is the one from the current blog post. In the second case, the <Field title /> tag isn’t just sitting by itself on a blog post, it’s sitting inside of a Loop tag. So each time the loop runs (once for each post in the query), the <Field title /> tag looks around, sees that it’s inside the current item in the loop (in this case, a new blog post each time the loop runs) and understands that the title field it should display is the one from the current blog post inside the loop. Every time you use a dynamic tag in L&L, the behaviour of that tag will be affected by the context it’s placed in.

Hopefully, by this point, you have an intuitive understanding of how dynamic tags get their context. But what happens when you’re not just placing a dynamic tag by itself on a page or inside a loop? If you use a dynamic tag as an attribute value (like <Loop type=post author="{Field name}">), where is that Field name tag getting its context? To illustrate that, take a look at the real-world L&L markup example below. Don’t worry if you don’t understand what’s going on at first glance, we’ll go over it piece by piece.

<h2>Recent posts by <Field author_full_name /></h2>
<ul>
  <Loop type=post author="{Field author}" count=5 orderby=date order=desc>
    <li>
      <a href="{Field url}"><Field title /></a>
      <Loop taxonomy=category post=current>
        <If check="{Field name}" value=best-practices>
          ⬅ Useful tips!
        </If>
      </Loop>
    </li>
  </Loop>
</ul>

If I placed this template on all blog posts over at loopsandlogic.com, it would output something like this if a post was written by me:

Recent posts by Benjamin Tracy

You can see that I’ve used the Field tag in several different places in that markup, but in each case, it’s getting field data from a different place on my site. It starts by getting Field author_full_name from the current post that this template is placed on, then further down it gets Field url and Field title from posts I’ve written, and then a bit further it checks the name of the category term associated with each of the blog posts using Field name. Let’s take a look at where each of those tags get their context.

<h2>Recent posts by <Field author_full_name /></h2>

Here, the <Field author_full_name /> tag is placed inside a template and that template could be placed on a page, post, or archive. In our example, we’ve decided to place the template on a blog post, so the tag will get its data from the current blog post on which the template has been placed.

<Loop type=post author="{Field author}" count=5 orderby=date order=desc>

We then open a loop and we’ve decided to use the author="{Field author}" query parameter to make it so that the loop only shows posts written by the same author as the post on which the template has been placed. Remember that tags get their context based on what’s around them. For the Field author tag in this case, that tag wouldn’t get its context from the loop since we haven’t actually opened our loop yet, we’re still just defining what we want to loop through. This means that Field author, just like Field author_full_name above, gets its context and data from the blog post on which we’ve placed our template.

But get ready because once we get inside the loop (everything between the > above and the closing </Loop> tag), we’ll be in a different context!

<a href="{Field url}"><Field title /></a>

All right, now we’re inside a loop, which means that these two Field tags get their context from the current item in the loop, not the current post on which we’ve placed the template.

<Loop taxonomy=category post=current>

And now we’re opening another loop; a loop within a loop! This one exists so that we can loop through the taxonomy terms of each blog post being displayed by our outer loop. Notice the query parameter post=current. Because this taxonomy term loop is inside a post loop, that parameter refers to the current post from the outer loop, not the current post that the template is placed on. Context is important!

<If check="{Field name}" value=best-practices>
  ⬅ Useful tips!
</If>

We’ve made it to the last bit of markup before we close all our tags. What we’re doing here is that for each taxonomy term in the taxonomy loop (the one we just opened above), we want to check whether the term matches “best-practices”. We’re doing this so that if ever one of the blog posts is in our blog’s Best Practices category, we can emphasize it with a little “:arrow_left: Useful tips!” note. Here again, we’re using Field name to get the name of the current item. Where does this tag get its context? From the taxonomy term loop that surrounds it!

Minor note: you would probably usually use the field=name attribute here instead of check="{Field name}", but I decided to do it this way to show an extra example of how context works in L&L.

Passing data between loops

We’ve seen in the examples above where tags placed in different parts of your L&L markup get their context. But what happens when you want to pass data from one loop to the inner content of another loop? This isn’t a complete example, but imagine something like this:

<Loop type=post>
  <Loop type=taxonomy_term>
    Here's some data from the post loop: <Field name />
    And here's some data from the taxonomy term loop: <Field name />
  </Loop>
</Loop>

Hopefully, by now you see that this structure wouldn’t work. In that example, both Field tags are in the taxonomy term loop, so they would both get their context from that inner loop. Without any additional L&L magic, it wouldn’t be possible to dynamically refer to the fields of the post loop. Luckily, L&L is full of magic. The solution is to work with variables using the Get and Set tags. Here’s how we would use the Get and Set tags to pass data from one loop to the inner content of another based on the example above:

<Loop type=post>
  <Set name=data_from_post_loop><Field name /></Set>
  <Loop type=taxonomy_term>
    Here's some data from the post loop: <Get name=data_from_post_loop />
    And here's some data from the taxonomy term loop: <Field name />
  </Loop>
</Loop>

Bonus tip

One last tip if you’ve made it this far: a useful way to visualize the context of your L&L (i.e. to check whether it’s placed directly on the template or if it’s placed inside of a loop) is to properly indent your markup just as you would if you were writing HTML. In the markup you’ve shared above, you haven’t indented properly so it isn’t obvious just from looking at your template that you actually have one of your loops nested inside the other. Fun fact about L&L’s template editor: if you select all of your template (ctrl+a), you can use shift+tab to make your code automatically indent itself correctly (assuming you’ve written all your opening and closing tags properly).

By the way, once you get to the point where you’ve figured out how to incorporate L&L into your workflow, it would mean the world to me if you’d consider writing a review for Loops & Logic on the WordPress plugin repo. We’re a small team trying to spread the word about this free tool and help empower people by providing support like this, so reviews help us a lot. If you’re not interested in that, no worries, I just thought I’d make a quick shameless plug :smile:

Best of luck with L&L!

1 Like