Wordpress如何将帖子摘录长度设置为100个字符和200个字符

时间:2010-08-11 14:39:39

标签: php wordpress

示例我想在两个位置使用the_excerpt。在functions.php我添加

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

问题。

如何让另一个函数接受另一个the_excerpt将是200个字符?

或其他解决方案。

如何制作这样的东西..动态the_excerpt()

  1. 选项1 = the_excerpt()限制为100个字符
  2. 选项2 = the_excerpt()限制为200个字符
  3. 选项3 = the_excerpt()限制为300个字符

1 个答案:

答案 0 :(得分:2)

您是否已阅读WordPress文档中的the_excerpt()

http://codex.wordpress.org/Function_Reference/the_excerpt

如上所述,您似乎只能使用new_excerpt_length()通过过滤器覆盖excerpt_length()。

因此,您可以修改现有的the_excerpt函数以获取参数$ length,其中默认值为null。如果你熟悉PHP,你知道你不必将任何参数传递给the_excerpt(),在这种情况下,$ length将默认为null并且就像$ length从未传递给它一样,这意味着当前所有the_excerpt()的用法将继续正常工作。如果您想以不同的长度打印摘录,请拨打the_excerpt(someOtherLength);

它看起来像这样。

function the_excerpt($length=null) {
     if($length != null) {
         // ignore default excerpt length with $length 
         // print the excerpt to $length 
     } else {
    // prints the excerpt to standard length specified by excerpt_length()
     }
}

如果您有任何问题,请告诉我。