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 specific number of entries per row

April 16, 2012 3:47pm

Subscribe [1]
  • #1 / Apr 16, 2012 3:47pm

    Jon Horton

    38 posts

    I know this has been addressed before, but I need a little help.

    Here’s what I’m trying to do using a channel:entries tag:

    <ul>
        <li>Entry 1</li>
        <li>Entry 2</li>
        <li>Entry 3</li>
        <li>Entry 4</li>
    </ul>
    
    <ul>
        <li>Entry 5</li>
        <li>Entry 6</li>
        <li>Entry 7</li>
        <li>Entry 8</li>
    </ul>

    For every group of 4 entries, I need to wrap them in a <ul> tag, for an unlimited/unknown total entries.

    I’ve tried this:

    {exp:channel:entries channel="test"}
    
    {switch="<ul>|||"}
       <li>{title}</li>
    {switch="|||</ul>"}
    
    {/exp:channel:entries}

    This method seems the best, and is very close to working; however, if the total number of results isn’t divisible by 4(ie, the last group only has 3 items in it), the last </ul> tag is never output, which causes the code to break.

    If I add the last tag using {if count = total_results}, that still doesn’t work because in instances where the total count is divisible by 4, I get double </ul> tags.

    I was really trying to get this to work natively, so please tell me if there is a way, or if I just need to use PHP instead.

    Thanks!

  • #2 / Apr 16, 2012 4:25pm

    Jon Horton

    38 posts

    Well dang, just figured this out.  Guess I was thinking about it too hard.

    Instead of using 2 switch tags, I just used one like:

    <ul>
      {exp:channel:entries channel='test'}
        <li>{title}</li>
        {switch="|||</ul><ul>"}
      {/exp:channel:entries}
     </ul>

    Just change the number pipes in the switch for the number of elements needed to be wrapped in each group.

    Awesome!

     

  • #3 / Apr 17, 2012 12:04pm

    Dan Decker

    7338 posts

    Hey Jon,

    NICE! - Honestly, I’ve been trying to sort out something similar. You sir, have helped me! Thanks!

    If you ever feel like chipping in again, just ping us in a new thread 😉

    Cheers!

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

ExpressionEngine News!

#eecms, #events, #releases