get_the_category

Retrieve post categories.

Description

(array) get_the_category( (bool) $id = false ); 

This tag may be used outside The Loop by passing a post id as the parameter.

Note: This function only returns results from the default category taxonomy. For custom taxonomies use get_the_terms().

Returns (array)

Array of WP_Term objects, one for each category assigned to the post.

Parameters (1)

0. $id — Optional. (bool) => false
Optional, default to current post ID. The post ID.

Usage

  1. if ( !function_exists( 'get_the_category' ) ) { 
  2. require_once ABSPATH . WPINC . '/category-template.php'; 
  3.  
  4. // Optional, default to current post ID. The post ID. 
  5. $id = false; 
  6.  
  7. // NOTICE! Understand what this does before running. 
  8. $result = get_the_category($id); 
  9.  

Defined (1)

The function is defined in the following location(s).

/wp-includes/category-template.php  
  1. function get_the_category( $id = false ) { 
  2. $categories = get_the_terms( $id, 'category' ); 
  3. if ( ! $categories || is_wp_error( $categories ) ) 
  4. $categories = array(); 
  5.  
  6. $categories = array_values( $categories ); 
  7.  
  8. foreach ( array_keys( $categories ) as $key ) { 
  9. _make_cat_compat( $categories[$key] ); 
  10.  
  11. /** 
  12. * Filters the array of categories to return for a post. 
  13. * @since 3.1.0 
  14. * @since 4.4.0 Added `$id` parameter. 
  15. * @param array $categories An array of categories to return for the post. 
  16. * @param int $id ID of the post. 
  17. */ 
  18. return apply_filters( 'get_the_categories', $categories, $id );