Network Security Internet Technology Development Database Servers Mobile Phone Android Software Apple Software Computer Software News IT Information

In addition to Weibo, there is also WeChat

Please pay attention

WeChat public account

Shulou

The method of adding words and reading time to WordPress topics

2025-02-25 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Servers >

Share

Shulou(Shulou.com)06/02 Report--

How to add the number of words and reading time to WordPress topics? This problem may be often seen in our daily study or work. I hope you can gain a lot from this question. The following is the reference content that the editor brings to you, let's take a look at it!

Add specific code for the number of words and reading time for the WordPress topic:

Article word statistics

/ / word count function zm_count_words ($text) {global $post;if (''= $text) {$text = $post- > post_content;if (mb_strlen ($output, 'UTF-8')

< mb_strlen($text, 'UTF-8')) $output .= '共' . mb_strlen(preg_replace('/\s/','',html_entity_decode(strip_tags($post->

Post_content)), 'UTF-8'). Word'; return $output;}}

The code is added to the current theme function template functions.php.

Article reading time

/ / Reading time function zm_get_reading_time ($content) {$zm_format = 'Reading time% min% minutes% sec% seconds'; $zm_chars_per_minute = 300; / / estimate the number of words per minute read $zm_format = str_replace ('% num%', $zm_chars_per_minute, $zm_format); $words = mb_strlen (preg_replace ('/\ s pencils)) (strip_tags ($content)), 'UTF-8')) $minutes = floor ($words / $zm_chars_per_minute); $seconds = floor ($words% $zm_chars_per_minute / ($zm_chars_per_minute / 60)); return str_replace ('% sec%', $seconds, str_replace ('% min%', $minutes, $zm_format));} function zm_reading_time () {echo zm_get_reading_time (get_the_content ());}

The code is added to the current theme function template functions.php.

Call the number of words of the article and the reading time code

Display the article word code:

Display the reading time code:

Add the above call code to the appropriate location of the current topic body template.

However, the word count and reading time are not very accurate, especially the reading time, which is bullshit, which is determined by CCTV announcer by default.

After writing this article, I found that there is more concise code on the Internet, the difference is that the above code is accurate to seconds, the following code only estimates points.

Function count_words_read_time () {global $post;$text_num = mb_strlen (preg_replace ('/\ s pointers), 'UTF-8'); $read_time = ceil ($text_num/300); / / modified numeral 300 adjustment time $output. =' Total in this article'. $text_num. One word, the expected reading time. $read_time. 'minutes.' ; return $output;}

Call code:

Thank you for reading! After reading the above, do you have a general idea of how to add words and reading time to WordPress topics? I hope the content of the article will be helpful to all of you. If you want to know more about the relevant articles, you are welcome to follow the industry information channel.

Welcome to subscribe "Shulou Technology Information " to get latest news, interesting things and hot topics in the IT industry, and controls the hottest and latest Internet news, technology news and IT industry trends.

Views: 0

*The comments in the above article only represent the author's personal views and do not represent the views and positions of this website. If you have more insights, please feel free to contribute and share.

Share To

Servers

Wechat

© 2024 shulou.com SLNews company. All rights reserved.

12
Report