【发布时间】:2017-12-30 21:22:13
【问题描述】:
我正在尝试在默认的 wordpress 博客页面上列出仅属于某个类别的帖子。我需要通过插件添加过滤器/动作钩子来做到这一点。我无法编辑主题模板文件。
我已使用以下代码为最近的帖子列表完成此操作:
add_filter('widget_posts_args', 'my_widget_posts_args');
function my_widget_posts_args($args) {
return array( 'posts_per_page' => 10,//set the number you want here
'no_found_rows' => true,
'post_status' => 'publish',
'ignore_sticky_posts' => true,
'cat' => 52 //$cat -> term_id//the current category id
);
}
我如何为帖子列表页面做同样的事情?
【问题讨论】:
-
到目前为止你做了什么?请添加代码
-
我正在创建简单的插件来过滤博客页面上的帖子。现在我需要在插件上编写代码并在帖子列表中按类别过滤帖子。我已使用代码 add_filter('widget_posts_args', 'my_widget_posts_args'); 从最近的帖子列表中删除function my_widget_posts_args($args) { return array( 'posts_per_page' => 10,//在这里设置你想要的数字 'no_found_rows' => true, 'post_status' => 'publish', 'ignore_sticky_posts' => true, 'cat ' => 52 //$cat -> term_id//当前类别id ); }
-
@RockyPrajapati 供将来参考,当有人要求提供代码时,请将其编辑到问题本身而不是放在评论中。这样更清晰。
标签: wordpress post filter hook