0

I have a html structure where two entries are grouped into one div, so what I do is this:

{exp:channel:entries channel="bullets" dynamic="no"}
     {switch='<div class="bullets">|'}
           <div class="col-lg-6 col-sm-6">
                                ...some content...
           </div>
     {switch='|</div>'}
{/exp:channel:entries} 

This works great as long as I have an even number of entries in the channel. If I have an odd number the closing is not being added.

I was wondering if I can somehow do something like this:

{if total_results mod 2 != 0}
   </div>
{/if}

to make sure I have a closing tag if I have an odd number of entries.

Any suggestions?

  • I solved it myself using the MX Calculator[1] Plugin and adding this here before the end of the loop: {!-- add a div if the number of entries is odd --} {if "{count}" == "{total_results}"} {if "{exp:mx_calc expression='mod({total_results},2)'}" != "0"} </div> {/if} {/if} – Steven M Aug 31 '14 at 21:03
4

Well, if you want a modulus operator, use a modulus operator!

{if total_results % 2 != 0}
   </div>
{/if}

Maybe you can find it easier this way:

{exp:channel:entries channel="bullets" dynamic="no"}
     {if count = 1 OR count % 2 == 1}
     <div class="bullets">
     {/if}
           <div class="col-lg-6 col-sm-6">
                                ...some content...
           </div>
     {if count == total_results OR count % 2 == 0}
     </div>
     {/if}
{/exp:channel:entries} 
| improve this answer | |

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.