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, '...<p><a class="more-link" href="' . get_permalink() . '" title="' . the_title_attribute('echo=0') . '"> ' . __('Read more »', '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(']]>', ']]>', $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.
Thank you very much. I’m kind of struggling with getting only excerpts on the main page. You opened my eyes a little bit.