Invalid excerpt_length limit digest length in WordPress

Recently, I upgraded WordPress to 6. x, and found that the summary length of the home page article list has changed from the previously specified length to the full text of the article, which scared me to check the data overnight for modification.

This article was published on the Zimiao haunting blog. Any reprint should follow the CC BY-NC-SA agreement, that is, the signature of this site, the original link of this site should be retained, and it should only be used for non-commercial or non-profit purposes.

phenomenon

Tip: WordPress has fixed this segmentation bug in subsequent updates. The description of this article is outdated. Please update the WordPress core program in a timely manner.

The article summary of the topic in the homepage list is output through the_exceprt method, and the excerpt_length hook is used to limit the length:

 echo the_excerpt(); // Output article summary code by Zimiao Appearing and Appearing Blog/azimiao.com
 //Hook Setting Summary Length (code by azimiao. com) add_filter("excerpt_length", "theExcerpt_Length"); function theExcerpt_Length($length) { return 155; }

On WordPress 5. x, the output content length meets the expectation; After upgrading to 6. x, the output content is very long, basically the full text of the article.

reason

Quite simply, in the excerpt_length hook and the_exceprt method, the specified length is not the string length, but the number of "words". The official document describes it as follows:

 apply_filters( 'excerpt_length', int $number ) //Filters the maximum number of words in a post excerpt.

In previous versions, a Chinese character seemed to be regarded as a word, so I mistakenly thought that the value was the string length and has been used since then.

In the new version, the English word separated by a space is recognized as a word, while a continuous Chinese paragraph is also recognized as a word (whether it is a bug or intentional). For an article full of Chinese, it is equivalent to only one word.

Of course, even if some English is mixed with Chinese, it is just a few words added after cutting, and the number is far below the 155 threshold, so the phenomenon of outputting full text appears.

Another extended question is that when asked how to limit the length of the abstract in Chinese search results, the answer is always the value of the excerpt_length hook, so few people (including me) go to see the original English document. An article copied back and forth, occupied the search engine, and finally misled a group of people.

resolvent

Tip: WordPress has fixed this segmentation bug in subsequent updates. The description of this article is outdated. Please update the WordPress core program in a timely manner.

No matter whether the phenomenon belongs to WordPress's own bug or is intentional, the problem must be solved. I temporarily use string trimming to avoid the problem. The code is as follows:

 echo the_excerpt_by_azimiao_com(155); function the_excerpt_by_azimiao_com($maxNum){ $originExcerpt = get_the_excerpt(); $originExcerpt = mb_substr($originExcerpt,0, $maxNum, 'utf-8') . '< a href="'.get_permalink($post->ID).' ">View more</a>'; return $originExcerpt; }

Since the site uses static plug-ins, most string trimming occurs when the cache is generated for the first time, so memory GC and performance issues will not be considered for the time being.

Zimiao haunting blog (azimiao. com) All rights reserved. Please note the link when reprinting: https://www.azimiao.com/8937.html
Welcome to the Zimiao haunting blog exchange group: three hundred and thirteen million seven hundred and thirty-two thousand

Comment

*

*

Comment area

  1. chyi 10-29 15:31 reply

    No way