Show ads after every 3 blog posts in archive

I want to place adsense ads after every 3 blog list items on archive page.
My archive page is made of tangible template. Is there any workaround to achieve this?

You can use loop count and if.

<Loop type="mycontent">
  <Set my_count><Get loop=count /></Set>
  <If variable=my_count in value="3,6,9,12,18"> // Or whatever the number of items on your page
    MYADD
  </If>
<Field title/>
</Loop>

Wrapped with proper css that should work.

1 Like

@PolarRacing’s got the right idea with conditional logic and <Get loop=count />, but there’s a slightly nicer way to achieve this using the Math tag and a modulus operator %. This isn’t an L&L-specific feature, but it is kind of an advanced math symbol used in programming that I wasn’t aware of until I saw another dev use it. Here’s a quick explanation of what that symbol does.

Basically, that mathematical symbol would allow you to divide the current loop count by 3 and then check if there’s anything leftover. If there’s nothing leftover, that means that the current loop count is a multiple of 3, so that would allow you to do something with your loop every third item.

In practice, that’d look something like this:

<Loop type=post>
  <If check="{Math}{Get loop=count}%3{/Math}" is value=0>
    This item's count is a multiple of three. Insert your adsense script here.
  </If>
</Loop>

My syntax above is quite compact, but if you want to see what’s actually going on with that % modulus operator, add something like this to your loop, which will help visualize that when the value of the operation is 0, that means it’s a multiple of 3:

<Loop type=post>
  The current loop count is: <Get loop=count />. 
  If we divide that by 3, there's this much leftover: <Math><Get loop=count /> % 3 </Math>
</Loop>

Edit: I’ve added this example to the docs along with an explanation of when it’s better to use the CSS :nth-child selector instead.