Take the 2-minute tour ×
WordPress Development Stack Exchange is a question and answer site for WordPress developers and administrators. It's 100% free, no registration required.

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?

share|improve this question
    
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 Sep 7 '12 at 4:37
1  
What have you tried already? Show us your code. And welcome to WordPress Stack Exchange! –  toscho Sep 7 '12 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;
share|improve this answer
    
Thanks this worked! –  icanhazgiraffe Sep 26 '13 at 20:19
    
@icanhazgiraffe please accept this answer. If you said this solution worked for you, mark it as such. –  Pieter Goosen Apr 2 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.

share|improve this answer
    
not sure you really need to drop down to SQL for this - what's wrong with using get_the_category() or similar? –  anu Oct 6 '12 at 9:51

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

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