如何在wordpress中的the_content()和the_excerpt()上设置字符限制?我只找到了字限制的解决方案 – 我希望能够设置输出的确切数量字符.
解决方法:
您可以使用Wordpress过滤器回调函数.在主题目录中,创建一个名为functions.php的文件,并在以下位置添加以下内容:
<?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_exercpt() – add_filter()执行相同的操作,然后使用函数作为回调.
有关详细信息,请参阅WordPress filter reference docs.