Blog / WordPress/ A Complete Guide to Getting Post Category Names and Slugs in WordPress

A Complete Guide to Getting Post Category Names and Slugs in WordPress

WordPress 获取文章所属分类名称与别名的完整指南

Introduction

In WordPress theme or plugin development, you often need to retrieve a post's category information, such as its name or slug. This guide details several standard methods to accurately obtain this data in different scenarios.

Getting Category Links and Names

WordPress core provides the convenient function the_category(), which outputs a list of links (as HTML hyperlinks) to the post's categories.

<?php the_category(); ?>

This function is suitable when you need to display linked category tags directly. However, sometimes you only need the plain text category name.

Getting the Current Category Name on Archive Pages

If you are on a category archive page (e.g., category.php), you can use the single_cat_title() function to get the name of the category being viewed.

<?php single_cat_title(); ?>

Getting Category Names on a Single Post Page

On a single post page (e.g., single.php), a post may belong to multiple categories. To get the names of all categories, loop through the array returned by get_the_category().

<?php
$categories = get_the_category();
if ( ! empty( $categories ) ) {
    foreach( $categories as $category ) {
        echo esc_html( $category->name ) . ' ';
    }
}
?>

Code Explanation:

  • get_the_category() returns an array of category objects.
  • The name property of each object is the category name.
  • Using esc_html() to escape output is a security best practice.

Getting the First Category's Name

If you only need the first (or primary) category name, use this code:

<?php
$categories = get_the_category();
if ( ! empty( $categories ) ) {
    $first_category = $categories[0];
    echo esc_html( $first_category->name );
}
?>

Getting the Category Slug

The category slug is the identifier used in the category URL, often used for building links or conditional logic.

Getting Slugs on a Single Post Page

<?php
$categories = get_the_category();
if ( ! empty( $categories ) ) {
    foreach( $categories as $category ) {
        echo esc_html( $category->slug ) . ' '; // Output slug
    }
}
?>

Getting the Current Category Slug on Archive Pages

<?php
if ( is_category() ) {
    $current_cat = get_queried_object(); // Get the current queried category object
    if ( $current_cat ) {
        echo esc_html( $current_cat->slug );
    }
}
?>

Correction & Optimization: The original code for getting the slug on a category page used get_query_var('cat') and get_category(). While functional, get_queried_object() is a more direct, modern, and standard method that returns the current category object.

Summary and Best Practices

The key to retrieving WordPress category information is knowing your context (post page vs. archive page) and the data you need (name, slug, or link).

  • Name: Use the category object's ->name property.
  • Slug: Use the category object's ->slug property.
  • Safe Output: Always escape text from the database using esc_html() to prevent XSS attacks.
  • Context Detection: Use conditional tags like is_category() and is_single() effectively.

Mastering these core functions and methods will allow you to handle and display category information flexibly in WordPress development.

Post a Comment

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