I’m using this little snippet of code to try and list the terms of a custom taxonomy for use in a meta box select box but it doesn’t show anything when I implement it (Despite working with the default ‘category’ taxonomy)
$partners = array();
$partners_obj = get_categories(array('taxonomy' => 'partner-cat'));
$partners[''] = '-';
foreach ($partners_obj as $partner) {
$partners[$partner->cat_ID] = $partner->cat_name;;
}
As you can see, my custom taxonomy is ‘partner-cat’ and there are posts in a custom post type in 2 separate terms of this taxonomy.
Any help would be most appreciated.
fetches taxonomies of type ‘categories’ particularly http://core.trac.wordpress.org/browser/tags/3.6/wp-includes/category.php#L0,
to fetch custom taxonomy you should use get_terms() instead, here http://codex.wordpress.org/Function_Reference/get_terms
Make sure you specify the proper slug you registered for taxonomy and change WP_DEBUG to true in your config file to check for further errors, as you might be fetching the taxonomy before you are registering it and hence no results in that case you’d get an error.
can you paste your code for registering taxonomy?
Are your taxonomy terms empty? They will not show by default. You could try using:
Note that
get_categories()
usesget_term()
to fetch your terms.Your
foreach
isn’t good too. you should accessterm_id
andname
instead.Whole code:
if you are using Custom Post Types CPT and I assume you’ve created custom taxonomy for that custom post type lets say:
You have CPType named Products and you have CPTaxonomy named Products Categories
If you are trying to display the CPTaxonomy into your products page somewhere in the loop you I would suggest to use this function according WP codex:
http://codex.wordpress.org/Function_Reference/get_terms
the code to get the CPTaxonomy for the CPType both named products should be the following:
then for displaying the taxonomies use this code:
The WordPress way:
This is using the direct implementation that WordPress uses in function get_categories() located within wp_includescategory.php