Getting key value of WP_Term object in wordpress

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;
}

Leave a Comment