Post Class for Posts with a “Read more” Link

Posted on

By Michael Fields

You might find that it is important to style a post differently if it contains a Read More tag. If you find yourself in this scenario, it is rather easy to add a new class to the post using the post_class filter.

function mfields_add_more_to_post_class( $classes ) {
	global $post;
	if ( ( is_archive() || is_home() ) && false !== strpos( $post->post_content, '<!--more-->' ) && ! in_array( 'more', $classes ) ) {
		$classes[] = 'more';
	}
	return $classes;
}
add_filter( 'post_class', 'mfields_add_more_to_post_class' );

Share your thoughts

*

Fork me on GitHub