Hack: Show Only First Paragraph of Post Without Using the More Tag

Ever write a long post and then publish it, only to realize later that you failed to include a <!–more–> tag, and you aren’t using an excerpt? The result could be that your entire post ends up on the front page, not just the first paragraph or a summary.

Sometimes to keep things simple I just want the first paragraph of every post to appear on the front page. But it is a pain to add those More tags every time. So here’s a filter you can add to your functions.php that looks at the content the front page is requesting, and then cuts it at the first return character in the post. No More tag needed.

add_filter('the_content', 'trim_content_for_index' ); 
function trim_content_for_index( $content ) {
	if (is_home()) {
		$content = substr($content,0,strpos($content,"\n"));
	}
	return $content;
}

The code simply uses the the_content filter and assigns a function to it. Then it checks is_home() to see if this is the home page. After all, you don’t want to cut the post short on the actual post page. Then it uses substr and strpos to grab the characters from the post up to the first newline character. It returns that as a substitute for the original content.

Instead of using is_home() you could go with is_front_page() or !is_singlular() and a variety of those things. That last one is good because it will also help for archive pages and other listings. But you may also want to check for is_archive() directly.

Leave a Reply

Your email address will not be published. Required fields are marked *