简体   繁体   English

如何在帖子页面显示WordPress的所有分类?

[英]How do I display all categories in WordPress on posts page?

I am trying to display all existing categories in WordPress. Not just the categories of one single post but all the categories that exist for the default post type.我正在尝试显示 WordPress 中的所有现有类别。不仅是单个帖子的类别,还有默认帖子类型存在的所有类别。 I know the functions get_categories() and wp_list_categories() , but they only display the categories of the first post in the loop when I'm on the posts page (basically the index.php).我知道函数get_categories()wp_list_categories() ,但是当我在帖子页面(基本上是 index.php)时,它们只显示循环中第一篇帖子的类别。 There must be a simple way to get around that but I just can't make it work.必须有一种简单的方法来解决这个问题,但我就是无法让它发挥作用。 Feeling a bit stupid by now.现在感觉有点傻。

I appreciate any help!感谢您的帮助!

Your reported scenario is because by default get_categories() is not returning the "empty" categories - categories that are not referenced by at least one post.您报告的情况是因为默认情况下get_categories()不返回“空”类别 - 至少一个帖子未引用的类别。 To get all categories, regardless of whether they are empty or not you should pass hide_empty as false , here is an example:要获取所有类别,无论它们是否为空,您都应该将hide_empty作为false传递,这是一个示例:

$categories = get_categories(['hide_empty' => false]);

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

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