get_category()
Gets data of the specified category.
If you pass a category object in the $category parameter, this data will be cached.
If you pass a category ID in the $category parameter, the category data will be retrieved from the database or from the cache if it exists.
The category data will be transformed to support backward compatibility (similar term parameters will be embedded in the array).
Note the function get_term(), which is identical to get_category. It can be used to get data from another taxonomy that is different from category.
Uses: get_term()
1 time — 0.000443 sec (fast) | 50000 times — 0.92 sec (very fast) | PHP 7.0.5, WP 4.5.1
No Hooks.
Returns
WP_Term|Array|WP_Error|null.
- Category data in the format specified in the $output parameter.
WP_Error- if the category is empty.null- if the category does not exist.
Usage
$cat = get_category( $category, $output, $filter );
- $category(number) (required)
- Category ID or object containing category data.
- $output(constant)
What type of array to return the data:
OBJECT— object;ARRAY_A— associative array;ARRAY_N— indexed array.
Default: OBJECT
- $filter(string)
- Filter (I didn't quite understand what filter).
Default: 'raw'
Examples
#1 Display the data of the category whose page we are on (the current one):
$cat = get_category( get_query_var('cat') );
// OR
if( is_category() ){
$cat = get_category( get_queried_object() );
}
print_r( $cat );
As a result, we will see something like this:
stdClass Object ( [term_id] => 85 [name] => Category Name [slug] => category-name [term_group] => 0 [term_taxonomy_id] => 85 [taxonomy] => category [description] => [parent] => 70 [count] => 0 [cat_ID] => 85 [category_count] => 0 [category_description] => [cat_name] => Category Name [category_nicename] => category-name [category_parent] => 70 )
Now we can get category data like so:
echo $cat->name; echo $cat->slug; echo $cat->count; // etc...
Changelog
| Since 1.5.1 | Introduced. |
get_category() get category code WP 6.8.3
function get_category( $category, $output = OBJECT, $filter = 'raw' ) {
$category = get_term( $category, 'category', $output, $filter );
if ( is_wp_error( $category ) ) {
return $category;
}
_make_cat_compat( $category );
return $category;
}