• Resolved matphoto

    (@matphoto)


    I set up a small 3-post loop on my static page to show the excerpts of featured posts. It’s working alright, but I want to separate them like so:

    post | post | post

    with a line between them. However, since the loop repeats all elements, it ends up like this:

    post | post | post |

    Is there a way to stop the last element from appearing like that? On a similar note, I’d also like to know how to alternate divs for each post in the loop, like this:

    begin loop
    <div class="1">Post</div>
    <div class="2">Post</div>
    <div class="1">Post</div>
    <div class="2">Post</div>
    end loop

Viewing 3 replies - 1 through 3 (of 3 total)
  • one of the ways is to use a counter variable;
    have you dividing border on the left side of the posts,
    and not show it for the first post:
    (this is not a copy/paste instruction)

    <?php $counter=0;
    ...query_posts...if you need it...
    if (have_posts()) :
    while (have_posts()) : the_post(); ?>
    <div class="main_post_wrap" <?php if($counter==0) { echo ' style="border-left:none;" ?>>
    ...post title, content, postmetadata etc. ...
    </div> <!--end of .main_post_wrap -->
    <?php $counter++; ?>
    <?php endwhile; endif; ?>

    similar method for your second question;
    using the modulus operator (a%2 – remainder of a division) with $counter:

    <?php $counter=0;
    ...query_posts...if you need it...
    if (have_posts()) :
    while (have_posts()) : the_post(); ?>
    <div <?php echo 'class="'.($counter%2+1).'"'; ?>>
    ...post title, content, postmetadata etc. ...
    </div> <!--end of .1 or .2 -->
    <?php $counter++; ?>
    <?php endwhile; endif; ?>
    Thread Starter matphoto

    (@matphoto)

    awesome, thanks I’ll try it tonight

    Thread Starter matphoto

    (@matphoto)

    worked perfectly, thanks a lot!

Viewing 3 replies - 1 through 3 (of 3 total)
  • The topic ‘Controlling how the loop repeats elements’ is closed to new replies.