How can I get an array with the names of all defined categories?
I need to retrieve an array with category names.
It see开发者_JS百科ms that tag names and category names are both stored in the same table (wp_terms
), and to distinguish them you need to look in the wp_term_taxonomy
table where the IDs from wp_terms are listed with options post_tag or category.
So I need to do some sort of relation query which, to be honest, is more than I can handle at this point. I did find that apparently there is some sort of a short cut to this. I haven't been able to find a good description of the function, but if you consider this: $wpdb->categories->cat_name` you may know what it's all about. I certainly don't.
How do I get an array with the names of all defined categories including nothing else?
You can use get_categories
for this.
http://codex.wordpress.org/Function_Reference/get_categories
This will fetch all categories
get_categories(array('hide_empty' => 0));
http://codex.wordpress.org/Function_Reference/get_categories
I find the documentation to be quite good actually.
edit:
if you just need it for a static menu use the menu panel, add support for it in your theme first:
add_theme_support( 'menus' );
I would have thought using get_categories();
would work for you. The documentation in the codex seems pretty straight forward. You can see exactly how get_categories()
is used with wp_list_categories()
A lot of times when I can't find the right documentation for it, I just google something like "Worpdress Codex Get 'xxx'" and I can find what I need.
Let us know if get_categories()
works for you.
http://codex.wordpress.org/Function_Reference/get_categories
精彩评论