I have two categories (our work and news) and only one single.php If looking at a single post in the news category then I want to show the thumbnail too so have inserted:

<?php if ( has_post_thumbnail() ) {the_post_thumbnail('medium');} ?>

But I dont want this to appear if viewing a single post within the our work category, what can I add to remove this for category ID#2?

2 Answers 2


You could do this :

$categories = get_the_category($post->ID);
$cat = $categories[0]->cat_name;

if ( has_post_thumbnail() && $cat == 'news' ) { the_post_thumbnail('medium'); } 

*EDIT: the code grabs the post category, line 2 grabs cat name from the object and I use it as a condition in if statement *

  • Please explain your code and why you think it will solve the problem rather than just posting the code itself.
    – s_ha_dum
    Jul 2, 2013 at 15:41

I would do it this way. It will work even if you assign multiple categories to posts.

if ( has_post_thumbnail() && has_category('news') ) {  // or !has_category('our-work')

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.