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

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!
Looking for WordPress hosting? Try WP Web Host. Prices starts at $5/month and you can try it for free!Replace full…

You Might Also Like...

  • WP Engineer Favicon

    WordPress 2.9 new excerpt filters

    370 readersIn WordPress 2.9 there will be two new filters to adjust an excerpt (the_excerpt). Previously with the_excerpt cut off at a maximum of 55 words and add a [...]. These filters are in WordPress 2.9 expandable. To change the values, you write two functions in your theme functions.php: // Changing excerpt length function new_excerpt_length($length) { return 40; } add_filter('excerpt_length', 'new_excerpt_length');   //

  • WordPress tip : change excerpt length depending of the category

    344 readersDo you ever wished to be able to modify the excerpt length based on which category you are on, without modifying your theme files? If yes, I'm pretty sure you'll be happy with that recipe.Looking for WordPress hosting? Try WP Web Host. Prices starts at $5/month and you can try it for free!WordPress tip :

  • How to display custom post types on your WordPress blog homepage

    1948 readersWordPress 3.0 will allow you to create custom post types, so what about being able to list those custom types on your blog homepage? This very useful piece of code will show you how you can do it.Looking for WordPress hosting? Try WP Web Host. Prices starts at $5/month and you can try it for

  • The manual Excerpt in WordPress. What, why, how, tips and plugins

    303 readersThe manual Excerpt in WordPress. What, why, how, tips and plugins.: Demetris has a written a very nice, well illustrated and explained tutorial on the WordPress excerpt and how to manipulate it to take full advantage of it. WordPress excerpts, which are not excerpts, make a WordPress site easier to browse and its content easier

  • WordPress hack: Display post thumbnail in your RSS feed

    2830 readersIntroduced in WordPress 2.9, get_the_post_thumbnail() allow you to display a thumbnail in your posts. But what about displaying post thumbnails in RSS feeds? Just read this recipe to know how to do.Looking for WordPress hosting? Try WP Web Host. Prices starts at $5/month and you can try it for free!WordPress hack: Display post thumbnail in

  • Show the password on post excerpts

    212 readersJustin Tadlock has written a nice tutorial on how to show a password form field instead of the default text “There is no excerpt because this is a protected post“. ¶

  • Showing the post password form for excerpts in WordPress

    779 readersHow to replace excerpts for password-protected posts in WordPress with a password form instead of the standard no excerpt message.

  • WordPress Filter Options

    Plugin Review: WordPress Filter

    294 readersDo you have filters set in your email program, be it Gmail, Yahoo! Mail, Hotmail or even Outlook? Anyone who uses filters knows how their ability to improve productivity by getting basic things done easily. So, why not take the power of filters and integrate it with our very own WordPress? This is what Matt

  • Display the_excerpt only if there is text

    365 readersHave you ever wanted to display the excerpt only if you write one? A simple couple lines of code can display the_excerpt any where you like.

  • Thomas Scholz

    How To List All Posts Of An Archive, A Category Or A Search Result

    59 readersArchive pages are usually paged according to your settings in options/reading. Sometimes you may want to offer a page with all posts for an archive (time, category, search result). You need: a separate address for the unpaged archive, a filter for the internal WordPress query and a link to your ‘all posts’ page. We put