Last Post Class

Adding a Class to the last WordPress Post

So the other day I as working on a WordPress theme where I was including bottom borders after every blog post. I liked the way this looked except for the last post.. In this case, the last post had a bottom border and didn’t look good with the navigation. The CSS selector :last-child wasn’t giving me the result I was looking for so I did some searching..

I found the following function that adds a class to the last post. For example, lets say you have a blog with 15 posts that displays 10 posts on the first page and 5 posts on the second page. If you use the following function, the 10th post on the first page and the 5th post on the second page will both have the class last.

Simply add the following function to your functions.php file.

To get it to work, you need to be using the post_class function within your WordPress loop.