Back WordPress

How to Add New Class to First Post in The Homepage

WRITTEN BY ON 31 Jan 2011
9,032 VIEWS • SHARES
1 comment

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');

Join the discussion

Comments will be moderated and rel="nofollow" will be added to all links. You can wrap your coding with [code][/code] to make use of built-in syntax highlighter.

1 comment
Ayaz Malik 6 years ago
didn't worked for me :(
Reply