How I can get from php on wordpress to a value of WP_Term object. I am using the next code to get the object:
$queried_object = get_the_category( get_queried_object_id());
But this return an Array like this.
Array
(
[0] => WP_Term Object
(
[term_id] => 12
[name] => Argentina
...
)
)
How I can get only the value for [name] key in this object from php in wordpress?
1 Answer
It returns an array because Posts can have multiple categories. You just need to get the item from the array whose name you want, ($queried_object[0]
for the first one), then get the value out of that the same way you would any PHP object:
$name = $queried_object[0]->name;
You should probably include some checks to make sure the post has a category before attempting to use the array or object like this:
$categories = get_the_category( get_queried_object_id() );
if ( ! empty( $categories ) ) {
$category = $categories[0];
$name = $category->name;
}