In WordPress the_excerpt()
displays the excerpt field value of the current post. If you do not provide any content in excerpt field to a post (in the post editor’s optional excerpt field), it will display a teaser which refers to the first 55 words of the post’s content.
Remove excerpt text if excerpt field is empty :
By default, if you have your theme set to display the excerpt and no excerpt content is entered, WordPress will just display the first 55 words teaser from the post. But if you want don’t want anything displayed then simply use the following code in functions.php file.
remove_filter( 'get_the_excerpt', 'wp_trim_excerpt' );
This code will remove the wp_trim_excerpt
filter from get_the_excerpt()
function.
Change WordPress post excerpt length:
You can use excerpt_length
filter to set a custom length of the excerpt to display. Remember its the number of words, not number of characters.
function wpboys_change_excerpt_length( $length ) {
// Return a desired excerpt length number
return 150;
}
add_filter( 'excerpt_length', 'wpboys_change_excerpt_length', 999 );
Change WordPress excerpt more text[…] :
When an excerpt is displayed you will see characters like this , ” […] ” at the end. You may want to change it to something meaningful, like a readmore link, or simply disable it.
- To remove the […] characters from excerpt output , add this filter code in functions.php file.
add_filter( 'excerpt_more', '__return_empty_string' );
-
To add a readmore link, we will use the same
experpt_more
filter as given below. Use the following code in functions.php file of your active theme.function wpboys_excerpt_readmore(){
return '<a href="' . get_the_permalink() . '">Read More »</a>';
}
add_filter( 'excerpt_more', 'wpboys_excerpt_readmore', 999 );
If you still have any questions, feel free to leave a comment here, and we will try to help you.
Hi, I’m trying to achieve the title of this post (How to remove Excerpt if left empty?) in the twenty nineteen theme. It doesn’t seem to work. I’ve added the following code to fucntions.php and it has no effect:
add_action( ‘init’, ‘no_excerpt_init’ );
function no_excerpt_init()
{
remove_filter( ‘get_the_excerpt’, ‘wp_trim_excerpt’ );
}
Thanks for the code works great excerpts are gone, but when I do use excerpt function I would like to add 3 (…)at the end, but your code doesnt work for me, would you know a different solution? The excerpt are all generated by WP.
Thanks, Arno
Try using the following code and let me know, if it works. 🙂
function wpboys_excerpt_readmore(){
return '...';
}
add_filter( 'excerpt_more', 'wpboys_excerpt_readmore', 999 );
Hi, What if I just want the full text to display? Would I simply add: add_filter( ‘excerpt_more’, ‘__return_empty_string’ );