Calling Category name without the link

I have looked to see if there is a simple function that only returns the name of a category on a post. I am looking to output it as a subheading for a list of posts but do not want it as a link and the the_category(); function only returns a link.

I also don't want it as a list. Any thoughts on how I can just get the "slug" of the category?

Topic child-theme categories Wordpress

Category Web


What you are looking for is get_the_category(). Retrieve a list of the categories, and then run a loop and only output their names:

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

This function returns an array of WP_Term objects. You can check the provided link for a list of available methods.

About

Geeks Mental is a community that publishes articles and tutorials about Web, Android, Data Science, new techniques and Linux security.