WordPress.org

Ready to get started?Download WordPress

Codex

Attention Interested in functions, hooks, classes, or methods? Check out the new WordPress Code Reference!

Function Reference/get category

Description

Retrieves category data given a category ID or category object.

If you pass the $category parameter an object, which is assumed to be the category row object retrieved the database. It will cache the category data.

If you pass $category an integer of the category ID, then that category will be retrieved from the database, if it isn't already cached, and pass it back.

If you look at get_term(), then both types will be passed through several filters and finally sanitized based on the $filter parameter value.

The category will converted to maintain backwards compatibility.

Note: use get_term() to get Link Categories based on their ID's. get_category only returns Post Categories.

Usage

<?php get_category$category$output$filter ?>

Parameters

$category
(integer|object) (required) Category ID or Category row object
Default: None
$output
(string) (optional) Constant OBJECT, ARRAY_A, or ARRAY_N
Default: OBJECT
$filter
(string) (optional) Default is raw or no WordPress defined filter will applied.
Default: 'raw'

Return Values

(mixed) 
Category data in type defined by $output parameter. Returns null if $category does not exist.

Examples

Print category data

$thisCat = get_category(get_query_var('cat'),false);

print_r($thisCat);

produces;

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
)

Notes

  • Uses: get_term() Used to get the category data from the taxonomy.
  • The count attribute includes custom post types as well if the custom post type uses standard categories.

Change Log

Source File

get_category() is located in wp-includes/category.php.

Related

See also index of Function Reference and index of Template Tags.