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.

On my WordPress theme, it displays all of the categories a post is in on the homepage for the post block, I only want to display one category even if a post is under multiple categories.

i.e. Category One, Category Two, Category Three

I want it to be Category One...

This is the code that is in place at the moment:

<h2><?php the_category(', ') ?></h2>
share|improve this question
add comment

1 Answer

Here's snippet to display first category from post.

$category = get_the_category();

if ( $category[0] ) {
    echo '<h2><a href="' . get_category_link( $category[0]->term_id ) . '">' . $category[0]->cat_name . '</a></h2>';
}

Original idea of the code is from WordPress Codex

share|improve this answer
    
When using this snippet of code, it doesn't simply any categories. It's simply blank. –  user1086337 Feb 6 '12 at 9:26
    
Are using it inside the loop? Otherwise you need pass post ID into get_the_category() –  Mamaduka Feb 6 '12 at 10:24
    
thanks a lote mate –  Sagive SEO Mar 7 '12 at 8:58
add comment

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.