There may be a need for a special appearance of the first post (for example, when you need to make a completely different design from the other posts), whether the main page, the archives, search, etc. This problem is solved by adding the special CSS-class with a simple PHP-code.
Initially, we have next standard wordpress loop to output our posts:
<?php if (have_posts()) : ?> <?php while (have_posts()) : the_post(); ?> <div class="post"> ... </div> <?php endwhile; ?> <?php endif; ?>
We are going to add class ‘last’ to last post as follows:
<?php if (have_posts()) : ?> <?php while (have_posts()) : the_post(); ?> <div class="post<?php if(($wp_query->current_post + 1) == ($wp_query->post_count)) echo ' last'; ?>"> ... </div> <?php endwhile; ?> <?php endif; ?>
Done! We can use class ‘last’ to highlight last post.
Both comments and pings are currently closed.