lsl
lsl

Reputation: 4419

Wordpress: check if current post is first in the loop

I need to style the first post in the loop differently from the rest in a wordpress theme template. Is there a specific wordpress function to check for this or do I just need to set a "first" flag like the below?

<?php while ( have_posts() ) : ?>

    <?php the_post(); ?>

    <?php if ( $first = !isset( $first ) ) : ?>

        <!-- First Post HTML -->

    <?php else : ?>

        <!-- Every other posts HTML -->

    <?php endif; ?>

<?php endwhile; ?>

The function I'm looking for would replace the $first = ! isset( $first) check. Does this function exist in WordPress?

Upvotes: 4

Views: 2052

Answers (1)

Vel
Vel

Reputation: 9257

Try this

<?php $Inc=0; ?>

<?php while ( have_posts() ) : ?>

    <?php the_post(); ?>

    <?php if ( $Inc==0 ) : ?>

        <!-- First Post HTML -->

    <?php else : ?>

        <!-- Every other posts HTML -->

    <?php endif; ?>
    <?php $Inc++; ?>
<?php endwhile; ?>

Upvotes: 1

Related Questions