Getting Wordpress custom taxonomy/category?
-
16-10-2019 - |
Question
Well, I'm using my own taxonomy for custom post type. It looks like this:
register_taxonomy("our_gallery", array("gallery"), array("hierarchical" => true, "label" => "Types", "singular_label" => "Type", "rewrite" => true));
I have created a few "types"/categories in my admin panel like "Black and white", "Landscapes" and so forth...
Anyways I can't figure out how to get these categories out? I mean - display them on my gallery page just next to gallery items?
I've tried:
<?php $cat = get_the_category();
var_dump($cat); ?>
And it displays an empty array - "array(0) { }". How to get access to my own categories? get_the_category('our gallery') and ('gallery') also gives an empty array...
Thanks a lot :)
Solution
I think you are getting confused by terminology here.
Category is a taxonomy
. Specific categories you create are terms
.
our_gallery is taxonomy
. Landscapes is term
. our_gallery is not category
. It is its own taxonomy
and has nothing to do with category taxonomy
.
get_the_category()
function explicitly fetches terms
of category taxonomy
.
To get terms
of our_gallery taxonomy
you need more generic function get_the_terms()
.
OTHER TIPS
Maybe you should use following statement to get categories of a taxonomy:
$taxonomy = 'your-taxonomy-name';
$tax_terms = get_terms($taxonomy, array('hide_empty' => false));