Reputation:
I have a custom post type in wordpress that I want to get the categories for so I can list them as filter options. I used
$terms = get_terms(array(
'post_type' => 'leadership',
'hide_empty'=> false,
));
to get an object but I am having difficulty figuring out how to get anything out of it.
a chunk of the object is:
Array ( [0] => WP_Term Object ( [term_id] => 3 [name] => Finance
I want the name portions.
I wrote $names = $terms->name;
but that doesn't seem to be doing the trick.
How exactly is this handled in php?
Upvotes: 0
Views: 105
Reputation: 406
Try the following code to fetch all categories $names=array_column($terms, 'name');
Upvotes: 0
Reputation: 3879
Use the below code
$terms = get_terms(array(
'post_type' => 'leadership',
'hide_empty'=> false,
));
// loop the results
foreach ( $terms as $term ) {
echo $term->name ;
}
Upvotes: 1