如何在Wordpress中设置the_content()和the_excerpt()的字符数限制 [英] How to set character limit on the_content() and the_excerpt() in wordpress
问题描述
如何在wordpress中设置the_content()和the_excerpt()的字符数限制?我只找到了字数限制的解决方案-我希望能够设置输出的确切数量的字符.
How do I set a character limit on the_content() and the_excerpt() in wordpress? I have only found solutions for the word limit - I want to be able to set an exact amount characters of outputted.
推荐答案
您可以使用Wordpress过滤器回调函数.在主题的目录中,创建一个名为functions.php
的文件,然后在以下文件中添加以下内容:
You could use a Wordpress filter callback function. In your theme's directory, create a file called functions.php
and add the following in:
<?php
add_filter("the_content", "plugin_myContentFilter");
function plugin_myContentFilter($content)
{
// Take the existing content and return a subset of it
return substr($content, 0, 300);
}
?>
每次通过the_content()
请求帖子/页面的内容时,都会调用plugin_myContentFilter()
函数-它为您提供内容作为输入,并将使用您从该函数返回的内容进行后续输出或其他过滤器功能.
The plugin_myContentFilter()
function will be called each time you request the content of a post/page via the_content()
- it provides you with the content as an input, and will use whatever you return from the function for subsequent output or other filter functions.
您还可以对the_exercpt()
-add_filter()
然后执行要用作回调的函数进行相同的操作.
You can also do the same for the_exercpt()
- add_filter()
and then a function to be used as a callback.
有关更多详细信息,请参见 WordPress过滤器参考文档.
See the Wordpress filter reference docs for more details.
这篇关于如何在Wordpress中设置the_content()和the_excerpt()的字符数限制的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!