简体   繁体   中英

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. 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). 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. To get all categories, regardless of whether they are empty or not you should pass hide_empty as false , here is an example:

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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