0

I've been looking for an answer to this question for a long time, but haven't found anything: how come when I use the_excerpt() on a given site it doesn't automatically print the first few characters of the content?

The excerpt only prints it if something has been added to the WordPress summary field of the post.

How can I fix this problem? With other sites this does not happen and if no text is added on the summary the excerpt is generated automatically.

What could it be that the excerpt is not automatically generated?

My situation is similar to this: Automatic Excerpt Not Working

but your response did not provide me with any advice unfortunately.

1 Answer 1

1

By default, the_excerpt(), doesn't uses the post/page content wysiwyg, it has its own textarea, that by default is not visible, you need to enable it first via screen options.

enter image description here

After you checked this checkbox you can scroll down to the bottom of the post/page and see a new textarea, this is the excerpt.

enter image description here

If you want to use the content (wysiwyg, the_conetnt() or get_the_content()) as excerpt, you will need to use wp_trim_words.
Something like this.

// the second argument is how many words to trim, default is 55
<?= wp_trim_words(get_the_content(), 30); ?>

Anothe option, if you want to keep the html structure would be to do this.

<?= force_balance_tags(html_entity_decode(wp_trim_words(htmlentities(wpautop(get_the_content())), 30))); ?>
5
  • I used the first code you mentioned and it works perfectly. But then how come on other sites where I use the_excerpt() and have the summary field empty it still works and prints the first 30 characters of the content? Oct 14, 2021 at 8:16
  • @MatteoFeduzi this could be because of a add_filter(...) that hooks into the_excerpt, you can change the default behaviour of many WP functions by hooking to the appropriate filters/actions Oct 14, 2021 at 8:26
  • My themes are all custom and they all have the same rules in the functions.php file, it seems very strange to me... Oct 14, 2021 at 8:40
  • @MatteoFeduzi Maybe you have a plugin installed that hooks into the_excerpt, if you take a look at the function in wordpress github, you can see that there is no mention of content anywhere, so this leaves one thing, somewhere in your theme/plugins, there is a add_filter('the_excerpt', 'some_callback') that changes the default behaviour. Oct 14, 2021 at 8:45
  • @MatteoFeduzi what I usually do in situations like this is, if the site is production (live), download the theme and plugins, using VS code do a global search for the filter and find where its called. Oct 14, 2021 at 8:47

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.