Wordpress Technology

Wordpress Tips, Tutorials, Hacks

Highlight Last Post

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.

Leave a trackback from your own site.

2 Responses to “Highlight Last Post”

  1. Hi, thanks for sharing your tips. May i ask you for a solution, as i dont have the skills in php.

    I’m using WordPress as a classifieds website where people can post their ads. I would like to highlight the latest ads. For example the latest ads (postings) in the past 24 hours or the latest in the past 7 days.

    I know what i want to set in the css, but i dont know how the php code would be. Can you give a example?

    Best Regards Rolf

  2. Helpful Thanks for the information . I found this website using google . I love this website very much and this is my first comment on this website . I have bookmarked it and come back . if you won’t to see my blog click here … Capital Web

Leave a Reply

You must be logged in to post a comment.

© 2009 | Made by Vancouver Web Design | Internet Marketing