Display WordPress post excerpt instead full content

The WordPress Excerpt is an optional summary or description of a post; in short, a post summary. After published your post, the excerpt will be display in RSS feeds (if you choose it’s not display your full post), Search results, Tag archives, Category archives, Monthly archives and Author archives. Don’t confused between excerpt and teaser (the part of a post that appears on the front page when you use the More tag). While both are related to the manual excerpt, they are different from it.
I recommend you using post’s excerpt in the loop of post instead full content with <!--more--> tag because you can introducing about your post better than automatic teasing a part from the content. Your post will be look more appealing.
By default, WordPress display the post content in loop with the_content() function in theme files, so your custom excerpt will not display as you want on the homepage (or blog page) without editing it. We can tell WordPress using the Excerpt instead the teaser by filter the_content() with your custom function in functions.php.
In my child theme’s functions.php file, I added this bit of code to tells WordPress that I want to filter the_content() with my function:

// Add filter to the_content
	add_filter('the_content', 'narga_excerpts');

Now, I write the custom function:

function narga_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;
        $content = strip_shortcodes($content);
        $content = str_replace(']]>', ']]>', $content);
        $content = strip_tags($content);
        # 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 = 50;
        $words = explode(' ', $content, $excerpt_length + 1);
        if(count($words) > $excerpt_length) :
            array_pop($words);
        array_push($words, '...

' . __( 'Read more »', 'narga' ) . '

'); $content = implode(' ', $words); endif; $content = '

' . $content . '

'; endif; endif; # Make sure to return the content return $content; }

What’s I can customize the excerpt content?

  • $excerpt_length = 50;: the number of words in excerpt, if the excerpt is not defined, it’ll cut down from the full content.
  • $content = strip_shortcodes($content);: remove all shortcode in excerpt
  • $content = str_replace(']]>', ']]>', $content);: remove [[ and ]] character from the begin and the end of excerpt
  • $content = strip_tags($content);: remove all HTML code

This function has been implemented of NARGA framework, you can change it likes you want.
There may be a better way to filter the_content(), and I’d love to see what you can come up with if so.

One thought on “Display WordPress post excerpt instead full content

  1. MacNoob

    Thank you very much. I’m kind of struggling with getting only excerpts on the main page. You opened my eyes a little bit.

    Reply

Leave a Reply

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

You may use these HTML tags and attributes: <a href="" title=""> <abbr title=""> <acronym title=""> <b> <blockquote cite=""> <cite> <code> <del datetime=""> <em> <i> <q cite=""> <strike> <strong>

Scroll To Top