Class “last” with counter

With this piece of code you can get posts with the get_posts function and if you have for example 3 divs next to each other and you want the third with a class=”last”, than you can use the following code:

<?php $post_counter = -2; ?>
<?php
$posts = get_posts('&cat=1&numberposts=5');
foreach( $posts as $post ) : ?>
<?php $post_counter++; ?>
<div class="classname <?php if( $post_counter == count( $posts )) { echo "last"; }?>">
</div>
<?php endforeach; ?>
Advertisements

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s