Blog / WordPress/ How to Get Post Category Name, ID, and Slug in WordPress

How to Get Post Category Name, ID, and Slug in WordPress

WordPress如何获取文章分类名称/ID/别名

When developing WordPress themes, you often need to retrieve category data such as name, ID, or slug. This article covers several common methods to get the category, ID, and slug for a post.

Using WordPress Core Functions

1. Display Category Name with Link

The the_category() function outputs the linked category name(s) for the current post.

<?php the_category(); ?>

If you need the plain category name without a link, use the methods below.

2. Get Category Information on Single Post Pages

Use get_the_category() to retrieve an array of category objects for the current post, then extract the needed information.

<?php
$categories = get_the_category(); // Get array of category objects
if (!empty($categories)) {
    echo $categories[0]->cat_ID; // Output first category ID
    echo $categories[0]->cat_name; // Output first category name
    echo $categories[0]->category_nicename; // Output first category slug
}
?>

To loop through all assigned categories:

<?php
foreach((get_the_category()) as $category) {
    echo $category->cat_name . ' ';
}
?>

3. Get Current Category on Archive Pages

On category archive pages (e.g., category.php), use these functions:

<?php
// Get the current category name
echo single_cat_title('', false);
// Get the current category ID
echo get_queried_object_id();
?>

Custom Helper Functions

Add these to your theme's functions.php for reusable logic.

// Get current category ID (for category archive pages)
function get_current_category_id() {
    $current_category = single_cat_title('', false);
    return get_cat_ID($current_category);
}

// Get the first category ID for the current post
function get_post_first_category_id() {
    $categories = get_the_category();
    if (!empty($categories)) {
        return $categories[0]->cat_ID;
    }
    return 0;
}

Usage:

<?php echo get_current_category_id(); ?>
<?php echo get_post_first_category_id(); ?>

Category Object Properties Reference

Category objects from get_the_category() or get_category() include these common properties:

cat_ID               // Category ID
cat_name             // Category name
category_nicename    // Category slug
category_description // Category description
category_parent      // Parent category ID
count                // Number of posts in category
taxonomy             // Taxonomy, default 'category'
term_id              // Alias for cat_ID

Example to get the slug: $category->category_nicename.

Best Practices and Considerations

  • Check Return Values: Always verify get_the_category() returns a non-empty array before using data to avoid errors on uncategorized posts.
  • Multiple Categories: A post can belong to multiple categories. get_the_category() returns an array; handle accordingly (e.g., take the first or loop through all).
  • Performance: Avoid calling category functions repeatedly inside loops. Store results in a variable for reuse.
  • Use Context-Appropriate Functions: Use get_the_category() on single posts, and get_queried_object() or single_cat_title() on category archives.

Using these methods, you can flexibly retrieve and display category information throughout your WordPress theme.

Post a Comment

Your email will not be published. Required fields are marked with *.