简体   繁体   English

WordPress-add_filter传递函数vars

[英]Wordpress - add_filter passing function vars

I have a premade function which looks like so: 我有一个预制的函数,看起来像这样:

function truncate($string='', $limit='150', $break=" ", $pad="...") {

I need to pass the $limit argument, but can't figure out how. 我需要传递$limit参数,但是不知道如何。
I'm calling the add_filter() as well, as follows: 我也调用add_filter() ,如下所示:

add_filter('the_content','truncate');

I want to pass 20 as the $limit . 我想通过20作为$limit

For the life of me, I can't figure how. 对于我的一生,我不知道怎么做。

Any help? 有什么帮助吗?

Cheers, 干杯,

I'm not sure if there's something for this within Wordpress, but the easy option is to create a new function: 我不确定在Wordpress中是否有此功能,但是简单的选择是创建一个新功能:

function content_truncate($string) { return truncate($string, 20); }
add_filter('the_content', 'content_truncate');

If you're using PHP >= 5.3 you might be able to use an anonymous function to make it a bit neater: 如果您使用的是PHP> = 5.3,则可以使用匿名函数使其更加整洁:

add_filter('the_content', function($string) { return truncate($string, 20); });

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM