I was working on a sidebar template and needed to get the category information for the page. Since I had the post ID, it was simple to get the category information with get_the_category(). When I pass in the post ID, it will return a WP_Term[] (an array of WP_Term objects).
In this example, I request all posts in the SQL category (lines 2-3). Then I call get_the_category() with the ID of the first post to get the category and write the contents to the web page with var_dump() (line 6).
// We only want the SQL category info so pass its slug. $args2 = array( 'category_name' => 'other-languages-sql' ); $query2 = new WP_Query( $args2 ); // Write the data directly to the web page. var_dump(get_the_category($query2->post->ID));
Below is the WP_Term[] array that is returned. This is the generated data for the post: SQL Variable, Temporary and Global Tables
object(WP_Term)#3244 (16) { ["term_id"]=> int(42) ["name"]=> string(3) "SQL" ["slug"]=> string(19) "other-languages-sql" ["term_group"]=> int(0) ["term_taxonomy_id"]=> int(42) ["taxonomy"]=> string(8) "category" ["description"]=> string(41) "SQL programming and diagnostic techniques" ["parent"]=> int(35) ["count"]=> int(2) ["filter"]=> string(3) "raw" ["cat_ID"]=> int(42) ["category_count"]=> int(2) ["category_description"]=> string(41) "SQL programming and diagnostic techniques" ["cat_name"]=> string(3) "SQL" ["category_nicename"]=> string(19) "other-languages-sql" ["category_parent"]=> int(35) }