0

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?

0

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.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.