I am trying to implement a slider on my site but i cannot find out how to display the category for each post displayed. I want it to display over the thumbnail. is there a code i can enter to display category?

  • A post can have multiple categories assigned to it. Are you working within the WordPress Loop? Perhaps, just a few more details on where you are having troubles would help to answer your question.
    – Brent
    Commented Sep 7, 2012 at 4:37
  • 1
    What have you tried already? Show us your code. And welcome to WordPress Stack Exchange!
    – fuxia
    Commented Sep 7, 2012 at 5:01

2 Answers 2


To get the current category of the post

just try this

$cats = get_the_category( get_the_ID() );

echo $cat_name = $cats[0]->name;
  • @icanhazgiraffe please accept this answer. If you said this solution worked for you, mark it as such. Commented Apr 2, 2014 at 6:18
function get_post_category($postId) {
    global $wpdb;
    if (!is_null($postId)) {
        $conditions = "AND p.id=" . $postId;
    } else {
        return false;
    $conditions .= " AND ttax.taxonomy='category'";
    $selectfields = "term.name name";
    $query = "SELECT " . $selectfields . "
                    FROM   wp_posts p 
                    INNER JOIN wp_term_relationships trel ON trel.object_id = p.id 
                    INNER JOIN wp_term_taxonomy ttax ON ttax.term_taxonomy_id = trel.term_taxonomy_id 
                    INNER JOIN  wp_terms term ON term.term_id = ttax.term_id  
                    WHERE 1=1 " . $conditions ." LIMIT 1";
    return $wpdb->get_results($query);

This function needs the post id as parameter and it will give you the top most category of the post, if you remove LIMIT, it will give you a list of all categories of the post. I am not sure whether you want achieve the same or not.

  • not sure you really need to drop down to SQL for this - what's wrong with using get_the_category() or similar?
    – anu
    Commented Oct 6, 2012 at 9:51

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

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