user4747724
user4747724

Reputation:

How do I get the property value of an object in php?

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

Answers (2)

HARI
HARI

Reputation: 406

Try the following code to fetch all categories $names=array_column($terms, 'name');

Upvotes: 0

Ravinder Reddy
Ravinder Reddy

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

Related Questions