在wordpress中限制搜索结果的内容长度

时间:2011-11-18 11:02:54

标签: php wordpress wordpress-theming

如何限制wordpress搜索结果中的内容长度或字符数限制?

我想要像the_excerpt()那样的东西。

我在页面中使用了get_template_part('loop', 'search')。那么我必须在search.phploop.php中更改。

有没有选择呢?

2 个答案:

答案 0 :(得分:1)

很多。我想起了substr。一个更复杂的解决方案可能是创建一个函数(在你的主题的functions.php中)shorten($content),它可以缩短文本并且:

  1. 剥离HTML标记
  2. 返回前n个字的字符串
  3. 在wordpress安装中查看wp-includes / formatting.php中的wp_trim_excerpt()函数作为示例。

答案 1 :(得分:1)

如何使用带有过滤器的_excerpt(),如codex中所述?

function new_excerpt_length($length) {
    return 20;
}
add_filter('excerpt_length', 'new_excerpt_length');

来源:http://codex.wordpress.org/Function_Reference/the_excerpt#Control_Excerpt_Length_using_Filters

相关问题