I’m trying to work out how to add a first and last class on the first and last items outputted from a while loop. The only thing I’ve found through searching has been relevant to working with mysql directly, while I’m using this in a WordPress loop (I’ve put in a function where I want to create the class osu_first_last()):
<div id="news-loop">
<h2 class="widget-title">News</h2>
<?php
// Build query for
$wp_news_query_temp = clone $wp_query;
$wp_news_query = new WP_Query();
$wp_news_query->query('category_name=News&showposts=3&orderby=date&order=DESC');
$news_counter = 0;
// Create posts loop
if ($wp_news_query->have_posts()) : while ($wp_news_query->have_posts()) : $wp_news_query->the_post(); ?>
<div class="news-entry news-entry-<?php echo $news_counter; ?><?php osu_first_last(); ?>">
<h3 class="entry-title">
<?php the_title(); ?>
</h3>
<?php twentyten_posted_dateonly(); ?>
<?php echo osu_short_excerpt(); ?>
</div> <!-- End div.news-entry -->
<?php
$news_counter++;
endwhile; ?>
<?php endif; $wp_query = clone $wp_news_query_temp; ?>
<a href="<?php bloginfo('url'); ?>/category/news/" class="sidebar-more">View all news</a>
</div>
Can anyone advise on the best way to do this please?
Thanks,
osu
ou can use the
current_postandpost_countand to determine the first and last post by passing it along with the query to osu_first_last()then implement osu_first_last() like this
In your code it would look like this: