ExpressionEngine CMS
Open, Free, Amazing

Thread

This is an archived forum and the content is probably no longer relevant, but is provided here for posterity.

The active forums are here.

Wrap channel entries in divs?

June 27, 2012 1:13pm

Subscribe [3]
  • #1 / Jun 27, 2012 1:13pm

    ryan777

    2 posts

    Is there a way to wrap every two channel entries inside a div? What I’m trying to do is create sort of a slider/carousel of entries from a channel, with only two showing at a time..the only way I can really do this is by having two channel entries inside blocks, sort of forming rows. I’m aware of the switch tag, but that alternates every other div. Anyone have any ideas? Thanks…

  • #2 / Jun 27, 2012 5:01pm

    Matthew81

    81 posts

    You might be able to do something with the {switch} tag:

    {exp:channel:entries channel="news"}
     {switch='<div>| '}
       <h1>{title}</h1>
     {switch='</div>| '}
    {/exp:channel:entries}

    I haven’t tested this, but it *seems* like it should work.


    EDIT: I just realized the second {switch} should probably look like this:

    {switch=' |</div>'}

  • #3 / Jun 28, 2012 6:41am

    John St-Amand

    865 posts

    What I have found works is to do this:

    {your entries loop}
         {switch="<div>|"}
              {your custom fields}
         {if count != total_results}
              {switch="|</div>"}
         {/if}
         {if count == total_results}
              </div>
         {/if}
    {/your entries loop}

    Switch is a great tool to insert whatever you want at repeating intervals.  The challenge is to also use it to close that loop when you need to, and not have it leave something when the loop doesn’t end on an even or odd instance.  Which is why I use the conditional above to apply switch and therein its pattern which MAY close the interval for all But the last entry in the loop.  I then separately ensure the closing of the patter by closing the div on the last entry - making sure the patter doesn’t create an unclosed div.

    Hope this helps.

.(JavaScript must be enabled to view this email address)

ExpressionEngine News!

#eecms, #events, #releases