Loop item number

Hi,

I’m trying to add a condition to a loop. What I need to do is loop through a Post Type which I am currently doing ok, I would like to get the current loop iteration number so I can set a CSS class against.

I would like to set if row number is odd set the background colour to something else.

IE
item 1: background-color: white;
item 2: background-color: red;
item 3: background-color: white;
item 4: background-color: red;

This is one of the things I would like to do based on a loop number.

Thanks in advanced.

Hi Tom, it can probably be achieved using the math tag but it would be better if this was natively supported by the loop tag.

https://docs.loopsandlogic.com/97/math?category_id=77

Hi Tom,
I would really recommend doing this with CSS :nth-child selectors instead of conditional classnames:

.item-class:nth-child(2n+1) { background-color: white; }
.item-class:nth-child(2n) { background-color: red; }

You can even simplify this further with the keywords “odd” and “even”, but understanding the first example will make these selectors useful to you in many more circumstances :slight_smile:

.item-class:nth-child(odd) { background-color: white; }
.item-class:nth-child(even) { background-color: red; }

Hope that helps!

1 Like

Thank you both for you replies.

For this example I will be using the nth-child Julia thank you, but also as Phil said I believe this should be added and supported natively supported by the loop tag as there are so many different case uses for a loop number. I will take a look at the Math workaround as well.

Use loop count from the loop variables Variables - Loops & Logic Knowledge Base

1 Like

As a few others have mentioned in this thread, the functionality you’re describing is already natively supported in L&L by using the loop count variable in conjunction with the Math tag if you want to use a different template on alternating loop items. That would look something like this:

<If check="{Math}{Get loop=count} % 2 {/Math}" is value=0>
  This item is even. <Template id=123 />
  <Else />
  This item is odd. <Template id=456 />
</If>

If you’re not already familiar with it, you’ll want to look up how the modulus operator % works.

While this is possible, it’s not the most efficient approach if you just want to style alternating items in the loop since with this approach you’re running some conditional logic and a mathematical calculation and getting a variable for every single item in the loop. That markup isn’t particularly taxing on the server, but it’s overkill if all you need to do is swap out a background color. If you need to render completely different data on alternating loop items then this would be a good approach, but for just styling stuff, it would be better to use CSS :nth-child selectors as Julia noted. Even if you did need to display different data on alternating items, it might still be more efficient to achieve this by hiding some pieces of data using CSS, but this would depend on the use case and would require some testing (probably with the Timer tag) to figure out which is the most efficient. So in your case, what I noted above wouldn’t be the right approach, but I thought I’d add it to this discussion anyway to show how this feature could be used.

I scanned the docs for a while trying to find this because I thought it must exist.

If the search function returned a bit more detail it would help:
https://docs.loopsandlogic.com/search?q=count

1 Like