• Hi,

    I’m creating my own theme and it’s almost finished. One thing more that I need to be able to do is set the class of the first post to something other than all the other posts. Are 2 loops essential for this or can it be achieved with one? When I tried two loops it seemed to mess up my navigation of older and newer entries.

Viewing 2 replies - 1 through 2 (of 2 total)
  • Moderator keesiemeijer

    (@keesiemeijer)

    at the beginning of the loop put this:

    <?php $count = 1; ?>
    <!-- loop starts here -->
    <?php while (have_posts()) : the_post(); ?>
      <?php $class = ($count==1) ? 'class="first"' : ''; ?>
        <div <?php echo $class; ?> id="post-<?php the_ID(); ?>">

    if you already have a class on your post change these lines

    <?php $class = ($count==1) ? ' first' : ''; ?>
        <div class="mypostclass<?php echo $class; ?>" id="post-<?php the_ID(); ?>">

    before the end of the loop you put this:

    <?php $count++; ?>
      <?php endwhile; ?><!-- end of loop -->

    OOPS – not quick enough!

Viewing 2 replies - 1 through 2 (of 2 total)

The topic ‘Manipulating the first post in the loop’ is closed to new replies.