Post Pic

Replace full post by excerpts on homepage without editing your theme files

In WordPress, switching from full posts to excerpt is easy if you edit your theme files. But do you know that you could achieve the same result without editing any of your theme file? Just read this recipe to find out.

What you have to do is to paste this code into your functions.php file and let WordPress filters do the work for you.

function my_excerpts($content = false) {
        // If is the home page, an archive, or search results
	if(is_front_page() || is_archive() || is_search()) :
		global $post;
		$content = $post->post_excerpt;

	// If an excerpt is set in the Optional Excerpt box
		if($content) :
			$content = apply_filters('the_excerpt', $content);

	// If no excerpt is set
		else :
			$content = $post->post_content;
			$excerpt_length = 55;
			$words = explode(' ', $content, $excerpt_length + 1);
			if(count($words) > $excerpt_length) :
				array_pop($words);
				array_push($words, '...');
				$content = implode(' ', $words);
			endif;
			$content = '<p>' . $content . '</p>';

		endif;
	endif;

// Make sure to return the content
	return $content;

}

add_filter('the_content', 'my_excerpts');

Thanks to Justin Tadlock for this cool snippet!

17 Responses

Jan 20 2010 08:47

Wow. What a long and useless way to do something that can be achieved by editing one line of code.

Jan 20 2010 10:41

Eric B. , you’re right, but this snippet also works if you haven’t set an excerpt or added a ‘read more’ tag at your articles.

Maybe it should be a hook to the get_excerpt() function, but this works just as good.

Jan 20 2010 20:08

Just because some people can lick themselves doesn’t mean it’s a good idea! A bit like this piece of code maybe?

Jan 20 2010 20:16

I take that back, maybe there is a use with child themes… still unsure about the licking thing though?

Jan 20 2010 23:42

I think this code is totally valid. Yeah it’s easier to modify the theme files, but for child themes this is perfect, as an intro to filters it’s a relatively simple example.

Jan 21 2010 08:27

Ben explained it all perfectly :) The fact it is not the easiest way doesn’t mean it is useless.

Jan 21 2010 21:49

What would make this really unique and useful is if it outputted only the first paragraph. Say you have a three paragraph post, it would find the first paragraph by finding the p tags and add the read more after that. I have been looking and working on a way to do that forever!

Jan 25 2010 10:35

@Montana: You can easily adept this function for your needs.
Just change the code under // If no excerpt is set

$content = $post->post_content;
if(preg_match_all(‘/(.*)/i’, $content, $matches)) {
$content = ” . $matches[1]. ”;
}

PS I haven’t tested it, but it should give you an idea of what I mean.

Jan 28 2010 07:45

Thanks for the code that really works :)

Apr 13 2010 09:34

Thanks for sharing. But instead I use Thumbnail for experts.

Apr 20 2010 16:28

To those that are saying to “modify the theme files” instead of adding chunks of code like this into your functions file I say this “pfft” what if over the time you have, 50? maybe more modifications to a theme?

And then you get the inevitable, BAM a major upate is released to your theme? what are you going to do then?

Install the update? and have to do all that work over again?

Or ignore the update? and have a potential security risk on your server?

It is far better to add code like this to your functions file, and make a few changes as posible to your theme files.

Aug 11 2010 17:21

When I add this to my function.php file do I need to add it between ?

Aug 11 2010 17:23

Sorry my comment did not format right before.

When I add this to my function.php file do I need to add it between

Trackbacks:

Leave a Comment

* Name, Email, Comment are Required