如何限制摘录中的字符长度?
2 个回答
- 投票数
-
- 2012-10-30
将这些行添加到function.php文件中
function custom_excerpt_length( $length ) { return 20; } add_filter( 'excerpt_length', 'custom_excerpt_length', 999 );
add these lines in function.php file
function custom_excerpt_length( $length ) { return 20; } add_filter( 'excerpt_length', 'custom_excerpt_length', 999 );
-
这将单词的数量限制为20,而不是字符.This limits the number of words to 20, not the characters.
- 9
- 2016-12-07
- Ionut
-
为什么我们在这里增加了数字999?Why we have added number 999 here?
- 0
- 2018-04-11
- Navnish Bhardwaj
-
@NavnishBhardwaj 999是要加载的过滤器的优先级.有关更多详细信息,请参见此处. https://developer.wordpress.org/reference/functions/add_filter/@NavnishBhardwaj 999 is the priority for the filter to be loaded. refer here for more details. https://developer.wordpress.org/reference/functions/add_filter/
- 1
- 2018-04-18
- Annapurna
-
- 2012-10-30
除了Deepa的答案提供的上述过滤器钩子之外,这里还有一个附加功能,可以帮助您以两种方式扩展
the_excerpt
的使用,允许您...
按字符数限制摘录,但不要截断最后一个单词.这样一来,您最多可以返回字符数,但保留完整的单词,因此仅返回可以在指定数量限制内的单词,并允许您指定摘录的来源.
function get_excerpt($limit, $source = null){ $excerpt = $source == "content" ? get_the_content() : get_the_excerpt(); $excerpt = preg_replace(" (\[.*?\])",'',$excerpt); $excerpt = strip_shortcodes($excerpt); $excerpt = strip_tags($excerpt); $excerpt = substr($excerpt, 0, $limit); $excerpt = substr($excerpt, 0, strripos($excerpt, " ")); $excerpt = trim(preg_replace( '/\s+/', ' ', $excerpt)); $excerpt = $excerpt.'... <a href="'.get_permalink($post->ID).'">more</a>'; return $excerpt; } /* Sample... Lorem ipsum habitant morbi (26 characters total) Returns first three words which is exactly 21 characters including spaces Example.. echo get_excerpt(21); Result... Lorem ipsum habitant Returns same as above, not enough characters in limit to return last word Example.. echo get_excerpt(24); Result... Lorem ipsum habitant Returns all 26 chars of our content, 30 char limit given, only 26 characters needed. Example.. echo get_excerpt(30); Result... Lorem ipsum habitant morbi */
此功能可以在整个主题文件中多次使用,每个主题文件均指定了不同的字符数限制.
此功能可以从任何一个中摘录
-
the_content
-
the_excerpt
例如,如果您在帖子编辑器屏幕的the_excerpt框中有包含文本的帖子,但想从the_content正文中摘录,则需要特殊的用例;
get_excerpt(140, 'the_content'); //excerpt is grabbed from get_the_content
这告诉函数您想要
the_content
中的前140个字符,而不管是否在the_excerpt
框中设置了摘录.get_excerpt(140); //excerpt is grabbed from get_the_excerpt
这告诉函数您要先从
the_excerpt
中获取前140个字符,如果不存在摘录,则the_content
将用作备用.可以对该功能进行改进以使其更加高效,或者可以将WordPress过滤器用于
the_content
或the_excerpt
,或者仅在存在以下情况的情况下按原样使用是不适合的内置WordPress API替代方案.In addition to the above filter hook supplied by Deepa's answer here is one additional function that can help you extend the use of
the_excerpt
in two ways,Allows you to...
Limit the excerpt by number of characters but do NOT truncate the last word. This will allow you to return a maximum number of characters but preserve full words, so only the words that can fit within the specified number limit are returned and allow you to specify the source of where the excerpt will come from.
function get_excerpt($limit, $source = null){ $excerpt = $source == "content" ? get_the_content() : get_the_excerpt(); $excerpt = preg_replace(" (\[.*?\])",'',$excerpt); $excerpt = strip_shortcodes($excerpt); $excerpt = strip_tags($excerpt); $excerpt = substr($excerpt, 0, $limit); $excerpt = substr($excerpt, 0, strripos($excerpt, " ")); $excerpt = trim(preg_replace( '/\s+/', ' ', $excerpt)); $excerpt = $excerpt.'... <a href="'.get_permalink($post->ID).'">more</a>'; return $excerpt; } /* Sample... Lorem ipsum habitant morbi (26 characters total) Returns first three words which is exactly 21 characters including spaces Example.. echo get_excerpt(21); Result... Lorem ipsum habitant Returns same as above, not enough characters in limit to return last word Example.. echo get_excerpt(24); Result... Lorem ipsum habitant Returns all 26 chars of our content, 30 char limit given, only 26 characters needed. Example.. echo get_excerpt(30); Result... Lorem ipsum habitant morbi */
This function can be used multiple times through out theme files, each with different character limits specified.
This function has the ability to retrieve an excerpt from either,
the_content
the_excerpt
For example, if you have posts that contain text in the_excerpt box on the post editor screen, but want to pull an excerpt from the_content body instead for a special use case you would instead do;
get_excerpt(140, 'the_content'); //excerpt is grabbed from get_the_content
This tells the function that you want the first 140 characters from
the_content
, regardless of whether an excerpt is set inthe_excerpt
box.get_excerpt(140); //excerpt is grabbed from get_the_excerpt
This tells the function that you want the first 140 characters from
the_excerpt
first and if no excerpt exists,the_content
will be used as a fallback.The function can be improved to be made more efficient and or incorporated with the use of WordPress filters for both
the_content
orthe_excerpt
or simply used as is in situations where there is no suitable, in-built WordPress API alternative.-
嗨!感谢所有提供的答案!我想问一下,如何使...而不是摘录结束时使用它?Hi! Thanks for all for the answer provided! I would like to ask, how to make it work with ... instead of [...] at the end of excerpt?
- 0
- 2012-11-02
- Jornes
-
@Jornes可能晚了6年,但这是省略号`&hellip;`的HTML代码.@Jornes it maybe 6 years late, but here is the HTML code for the ellipsis `…`
- 1
- 2018-07-20
- AlbertSamuel
-
@AlbertSamuel谢谢您的回答.:)@AlbertSamuel Thank you for the answer. :)
- 1
- 2019-05-10
- Jornes
阅读这篇文章后,我有一个问题(如何突出显示搜索没有插件的条款).我非常喜欢此功能(不带插件的搜索字词),但是字符长度太长.我应该添加哪些PHP代码以使摘要更短?如果有人可以建议,将不胜感激.谢谢!