开发者

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.

I'm calling the add_filter() as well, as follows:

add_filter('the_content','truncate');

I want to pas开发者_如何学Pythons 20 as the $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:

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:

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

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜