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/wp get post categories

Contents

Description

The function wp_get_post_categories() retrieve a list of categories for a post.

Compatibility layer for themes and plugins. Also an easy layer of abstraction away from the complexity of the taxonomy layer.

Usage

<?php wp_get_post_categories$post_id$args ); ?>

Parameters

$post_id
(integer) (optional) The Post ID.
Default: 0
$args
(array) (optional) Overwrite the defaults.
Default: array

Default $args are:

$defaults = array('fields' => 'ids');

Return Values

(array) 
The array contains a list of category ID's.

Examples

The example below shows how categories are retrieved, and then additional information is retrieved for each category.

$post_categories = wp_get_post_categories( $post_id );
$cats = array();
	
foreach($post_categories as $c){
	$cat = get_category( $c );
	$cats[] = array( 'name' => $cat->name, 'slug' => $cat->slug );
}

Notes

Change Log

Since: 2.1.0

Source File

wp_get_post_categories() is located in wp-includes/post.php.

Related

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