最新消息:20210917 已从crifan.com换到crifan.org

【已解决】wordpress中最近文章中只显示特定分类下的文章

显示 crifan 568浏览 0评论
一个wordpress的网站,希望在最近文章中,只显示category为某个特定分类的文章:
然后去研究。
知道去搜:
Recent Posts
找到了:
然后注意到了
(1)widget_posts_args函数
看名字就和widget的post有关系,是和args有关的
(2)WP_Query函数
查询的wp的
(3)一堆参数:
array(
      'posts_per_page' => $number,
      'no_found_rows' => true,
      'post_status' => 'publish',
      'ignore_sticky_posts' => true,
    )
很明显是和:
后台的小工具的近期文章的设置项有关系:
先去搜:
widget_posts_args
只搜到这一处,找不到其他的了。
去网上搜:
widget_posts_args
Plugin API/Filter Reference/widget posts args « WordPress Codex
add_filter('widget_posts_args', 'filter_recent_posts_widget_parameters');
function filter_recent_posts_widget_parameters($params) {
   $params['orderby'] = 'date';
   return $params;
}
貌似可以自己去添加:
add_filter
的filter去添加额外的参数的过滤的?
但是具体添加哪些参数,表示可以指定分类category的呢?
Class Reference/WP Query « WordPress Codex
当时没注意到。
后来的后来,注意到了,其中有:
“Category Parameters
Show posts associated with certain categories.
* cat (int) – use category id.
* category_name (string) – use category slug.
* category__and (array) – use category id.
* category__in (array) – use category id.
* category__not_in (array) – use category id.”
widget_posts_args | Hook | WordPress Developer Resources
widget_posts_args | filter | WordPress | hookr.io
Recent posts widget : how to display only the posts of the current category ? – Press Customizr
widget_posts_args (WordPress Filter Hook)
widget_posts_args (WordPress Filter Hook)
然后去搜:
widget_posts_args
的其中一个参数
wordpress/wp-includes/class-wp-query.php
然后发现其中有关于category的参数:
发现其中的category_name的解释是:
“   * @type string $category_name Use category slug (not name, this or any children).”
貌似是我此处需要找的:
category的参数,且可以指定category的slug别名的
然后继续搜代码:
wordpress/wp-includes/class-wp-query.php
          if ( $the_cat ) {
            $this->set( 'cat', $the_cat->term_id );
            $this->set( 'category_name', $the_cat->slug );
          }
确定category_name就是category的slug了。
然后就尝试着去添加默认参数,改为此处想要添加的一个分类:
slug别名是self_news:
      'category_name'       => 'self_news',
然后wordpress好像可以动态加载这些文件
-》所以无需重启wordpress所在服务器,即可生效了
-〉果然是可以的:
过滤后只显示对应分类的文章了:
而此时,之前靠假的,让上面那个帖子的修改时间为最新的,
且最近文章只显示1个,所以才能显示的。
现在就可以去修改为了:
显示个数为很多,比如20
也不会有其他影响,始终只显示这个分类下面的帖子了。
【总结】
此处,最终是通过去修改:
/wp-includes/widgets/class-wp-widget-recent-posts.php
中,给WP_Query的widget_posts_args的参数列表加上category_name,值设置为想要制定的单个分类的别名:
    $r = new WP_Query( apply_filters( 'widget_posts_args', array(
      'posts_per_page' => $number,
      'no_found_rows' => true,
      'post_status' => 'publish',
      'ignore_sticky_posts' => true,
      'category_name'       => 'self_news',
    ), $instance ) );
即可。
然后再回头看前面的帖子,其实貌似理论上可以:
不去修改wordpress的代码,而去加上对应的hook/filter,手动制定分类的别名,估计也是可以的。
大概的代码,估计是:
add_filter('widget_posts_args', 'filter_recent_posts_widget_parameters');
function filter_recent_posts_widget_parameters($
args
) {
   $
args
['
category_name
'] = '
self_news
';
   return $
args
;
}
但是之后怎么加进去(后来好像知道了是 添加到自己的child theme中?),以及是否还要设置其他参数,则就不是太清楚了,暂时也懒得继续研究了。等有需要再去优化为使用这个add_filter的方式去实现指定特定分类的效果。
【后记】
才看到:
widget_posts_args | Hook | WordPress Developer Resources
这里就是我们要的:category_name
另外,想要了解其他还有哪些参数,除了前面看代码,也可以看前面的:
Class Reference/WP Query « WordPress Codex
里面有详细的解释,尤其是:
“Category Parameters
Show posts associated with certain categories.
* cat (int) – use category id.
* category_name (string) – use category slug.
* category__and (array) – use category id.
* category__in (array) – use category id.
* category__not_in (array) – use category id.”

转载请注明:在路上 » 【已解决】wordpress中最近文章中只显示特定分类下的文章

发表我的评论
取消评论

表情

Hi,您需要填写昵称和邮箱!

  • 昵称 (必填)
  • 邮箱 (必填)
  • 网址
90 queries in 0.186 seconds, using 23.35MB memory