NARGA

  • Home
  • Categories
    • Articles
    • jQuery
    • CSS
    • How-to
    • Inspiration
    • Productivity
    • General
  • Freebies
  • WordPress
    • NARGA WordPress Library
You are here: Home / How-to / Display WordPress post excerpt instead full content

Display WordPress post excerpt instead full content

How-to, WordPress Last Updated on November 2, 2016 1 Comment

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.

This functions was deprecated because WordPress 3.3 introduced wp_trim_words() function.

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, '...<p><a class="more-link" href="' . get_permalink() . '" title="' . the_title_attribute('echo=0') . '">  ' . __( 'Read more &#187;', 'narga' ) . ' </a></p>');
        $content = implode(' ', $words);
endif;
$content = '<p>' . $content . '</p>';
endif;
endif;
# Make sure to return the content
return $content;
    }

What’s I can customize the wordpress post 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(']]&gt;', ']]&gt;', $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 Core Library WordPress Theme, you can change it likes you want.

Use wp_trim_words() to trim your text as you want

The function works like so:

  • $text: (string) (required) Text to trim. Default: None
  • $num_words: (integer) (optional) Number of words. Default: 55 .
  • $more: (string) (optional) What to append if $text needs to be trimmed. Default: ‘…’

If you wanted to display a trimmed version of your content you could do it like so:

It’s a great function that allows you to automatically trim certain text elements within your WordPress theme.
There may be a better way to filter the_content(), and I’d love to see what you can come up with if so.

Related

Tagged: wordpress child theme, wordpress tips and tricks

About NARGA Editorial

The author avatar

NARGA.NET is a website (blog) about web development, graphic and WordPress related. Do you interesting with our website to post your own post or place your advertise? Contact us now!

Follow me:

Comments

  1. MacNoob says

    June 16, 2013 at 7:01 AM

    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 Cancel reply

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

This site uses Akismet to reduce spam. Learn how your comment data is processed.

Highly Recommended

Divi 3 is the Ultimate Multi-Purpose Theme

Divi 3.0 – Best Drag Drop WordPress Theme, FREE Layout Kits

As you known, Divi is a "flag-ship" WordPress of Elegant Themes because it's named as Multi-Purpose … Read More...

Woocommerce Visual Attributes & Options Swatches

10 WordPress Plugins to Improve WooCommerce Product Variations

There are several WordPress plugins for WooCommerce to Improve WooCommerce Product Variations … Read More...

HandHeld Mobile Plugin

3 Fantastic Plugins to Optimize WordPress Mobile Friendly

In 2009, I listed some WordPress plugins to make your website mobile friendly. Just over 6 years and … Read More...

WooCommerce JetPack

Powerfull WordPress Plugins to Enhance WooCommerce Functions

WordPress and WooCommerce are free, you don’t have to spend anything to build your own web store and … Read More...

WooCommerce PDF Invoice

Top 7 WordPress Plugins to Invoice Your Clients

There are dozens of WordPress invoice plugins which you can use to turn WordPress into an excellent … Read More...

About NARGA.NET

NARGA is a blog dedicated to share top quality open source resources for web developer and web designer weekly (then daily asap). As a web designer or developer, you’ll find some of the best free icons, stock photos, fonts, free WordPress themes, userful WordPress plugins, best jQuery plugins, CSS3 snippets, HTML5 standard and a lot more …

Connect with Us

  • Write for Us
  • Follow us on Twitter
  • Become a Facebook Fan
  • Subscribe RSS Feed
  • Browser Post Archive
  • Terms & Conditions
  • Privacy Policy & Disclaimer
  • Contact NARGA

This site is licensed under Attribution-NonCommercial-ShareAlike 3.0 license. Feel free to change, reuse modify and extend it. Some authors will retain their copyright on certain articles.
NARGA.NET utilizes affiliate links and may receive a comission if you click an affiliate link and make a purchase.

  • Archives
  • Contribute
  • Terms & Conditions
  • Contact NARGA
Copyright © 2019 · NARGA GPRESS - All rights reserved. Proudly powered by Digital Ocean · Genesis Framework · WordPress · Log in