Wordpress如何将文章摘录长度设置为100个字符和200个字符 [英] Wordpress how to set the post excerpt length to 100 characters and 200 characters
问题描述
示例我想在两个位置使用 the_excerpt
.在 functions.php
我添加
Example I want use the_excerpt
in two locations. On functions.php
I add
function new_excerpt_length($length) {
return 100;
}
add_filter('excerpt_length', 'new_excerpt_length');
问题.
如何让另一个函数接受另一个 the_excerpt
将是 200 个字符?
How to make another function to accept another the_excerpt
will be 200 characters?
或其他解决方案.
如何制作这样的东西..动态 the_excerpt()
How to make something like this.. Dynamic the_excerpt()
- 选项 1 =
the_excerpt()
限制为 100 个字符 - 选项 2 =
the_excerpt()
限制为 200 个字符 - 选项 3 =
the_excerpt()
限制为 300 个字符
- Option 1 =
the_excerpt()
limit to 100 chars - Option 2 =
the_excerpt()
limit to 200 chars - Option 3 =
the_excerpt()
limit to 300 chars
推荐答案
你是否通读了 the_excerpt() 的 WordPress 文档
Have you read through the WordPress documentation for the_excerpt()
http://codex.wordpress.org/Function_Reference/the_excerpt
您似乎只能通过您提到的过滤器使用 new_excerpt_length() 覆盖 excerpt_length().
It appears that you can only override the the excerpt_length() with new_excerpt_length() via filters as you mentioned.
所以也许你可以修改现有的 the_excerpt 函数来接受一个参数 $length,其中默认值 = null.如果您熟悉 PHP,您就会知道不必向 the_excerpt()
传递任何参数,在这种情况下,$length 将默认为 null 并且其功能就像 $length 从未传递给它一样,这意味着 the_excerpt() 的所有当前用法将继续正常工作.如果您想以不同的长度打印摘录,请调用 the_excerpt(someOtherLength);
So perhaps you can modify existing the_excerpt function to take a parameter $length, where the default value = null. If you are familiar with PHP you know that you don't have to pass any parameters to the_excerpt()
, in which case $length will default to null and function as if $length was never passed to it, which means all current usages of the_excerpt() will continue working as normal. and if you want print your excerpt at a different length then call 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()
}
}
如果您有任何问题,请告诉我.
Let me know if you have any problems.
这篇关于Wordpress如何将文章摘录长度设置为100个字符和200个字符的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!