[英]add a filter to a sidebar widget ,wordpress posts_per_page
Hi Im wondering how to alter the number of posts_per_page
. 您好,我想知道如何更改
posts_per_page
的数量。 The widget()
function in default-widgets.php contains this line.. default-widgets.php中的
widget()
函数包含此行。
$r = new WP_Query( apply_filters( 'widget_posts_args', array( 'posts_per_page' => $number, 'cat' => 1, 'no_found_rows' => true, 'post_status' => 'publish', 'ignore_sticky_posts' => true ) ) );
The variable $number
is set right before this line as 10 but I would rather insert my own filter for posts_per_page
and change it to 1. 变量
$number
在此行之前设置为10,但我宁愿为posts_per_page
插入自己的过滤器并将其更改为1。
But Im unsure how to add a filter to this, im only learning about hooks and filters at the moment. 但是我不确定如何为此添加过滤器,只是目前仅了解钩子和过滤器。 As far as I know there is an array with
posts_per_page
but I dont know how to change this. 据我所知有一个具有
posts_per_page
的数组,但是我不知道如何更改它。
function recent_post_count() {
$query->set('posts_per_page', 1);
}
add_filter( 'widget_posts_args', 'recent_post_count', 6);
You're using the wrong argument. 您使用了错误的参数。
function recent_post_count($args) {
$args['posts_per_page'] = 5;
return $args;
}
add_filter( 'widget_posts_args', 'recent_post_count');
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.