Code Snippet > PHP

How to Add New Class to First Post in The Homepage

Sometimes, wordpress theme developers or users want to make their latest post looks different from the rest for their theme. Maybe they want to make the font larger, or the background color lighter, or whatever. This can be done by adding new class to the latest post and then styling the class via css. Here, I’ll share the trick to add that new class.

PHP

On standard themes, usually using post_class() function to create post classes. We need a filter so this function on the first post has a special class. Ok, Add the code in the function.php

function firstpost_class($class) {
	global $post, $posts;
	if ( is_home() && !is_paged() && ($post == $posts[0]) ) $class[] = 'firstpost';
	return $class;
}
add_filter('post_class', 'firstpost_class');


Source: http://www.dynamicwp.net/articles-and-tutorials/how-to-add-new-class-to-first-post-in-the-homepage/



Show Some Love, Spread This Post!

1 comment

Ayaz Malik Tue, 8th February 2011 didn't worked for me :(
Reply

Leave a comment

Have something to say? Drop a comment! No HTML tags are allowed in the comment textfield.

Advertisement