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'm setting up a site with a theme that takes advantage of the "featured image" feature of WordPress. Since my end users are not the most tech saavy in the world, I'd like to set things up so that the categories would have a default "featured image" assigned to the post. In the event of multiple categories assigned to a post, then it would simply take the first one.

Is there an existing plugin or a way to code this?

share|improve this question
add comment

7 Answers

up vote 12 down vote accepted

In your template where you have the Featured Image displayed: <?php the_post_thumbnail( 'thumbnail' ); ?> you can make it conditional on whether the Featured Image is set, then have it default to whatever you want if it's not set.

One way to do this is to put all the default images in a directory and name them for the categories, eg. news.jpg and reviews.jpg then instead of using <?php the_post_thumbnail( 'thumbnail' ); ?> to display your Featured Image you'd use this:

<?php 
   if (  (function_exists('has_post_thumbnail')) && (has_post_thumbnail())  ) { 
      the_post_thumbnail('thumbnail');
   } else { ?>
      <img src="whatever/directory/<?php $category = get_the_category(); echo $category[0]->cat_name; ?>.jpg" /> <?php }
   endif;
} ?>

So in this example above if the post is in the news category and your writer didn't set the Featured Image it'll default to the image stored at http://www.yoursite/whatever/directory/news.jpg.

share|improve this answer
    
This is how I do it as well. +1 –  epaps Feb 7 '11 at 0:08
    
what happened if a post belongs to a few categories ? –  Eray Mar 20 '11 at 0:06
    
Good question. get_the_category() will only return one category and I forget how it decides which category to use. –  matt Mar 20 '11 at 3:52
1  
get_the category() is getting all categories as an array, but you need a liitle trick . studiograsshopper.ch/code-snippets/… –  Eray Mar 23 '11 at 22:54
add comment

A cleaner way to do the above ;)

<?php if (  (function_exists('has_post_thumbnail')) && (has_post_thumbnail())) : ?>

<?php the_post_thumbnail('thumbnail'); ?>

<?php else :?>

<img src="whatever/directory/<?php $category = get_the_category(); echo $category[0]->cat_name; ?>.jpg" /> 

<?php endif;?>
share|improve this answer
add comment

There's http://wordpress.org/extend/plugins/dirtysuds-category-thumbnail/ too

share|improve this answer
    
Hey! It looks like they finally came out with a plugin to do this. Good find! I'll have to check it out later. Right now everything is working nicely with the code. –  Dillie-O Mar 18 '11 at 23:16
1  
This plugin looks for the next most recent post in the same category that has a featured image and displays the image. This will not set a default image for the entire category. Eg: what happens if you have a post about grapes with a pic of grapes as the featured image and then the next post is of oranges? You wouldn't want the grapes image showing up for your oranges. Simplistic example, I know. I just don't think this is a good option. –  joshmax Nov 16 '11 at 15:36
add comment
<?php if (  (function_exists('has_post_thumbnail')) && (has_post_thumbnail())) : ?>

<?php the_post_thumbnail('thumbnail'); ?>

<?php else :?>

<img src="<?php bloginfo('template_directory'); ?>/your image directory name in theme folder/<?php $category = get_the_category(); echo $category[0]->cat_name; ?>.jpg" /> 

<?php endif;?>
share|improve this answer
add comment

I ran into some problems with category names having spaces i altered the above code a bit to fit my needs

<?php if (  (function_exists('has_post_thumbnail')) && (has_post_thumbnail())) : ?>

<?php the_post_thumbnail('thumbnail'); ?>

<?php else :?>

<img src="whatever/directory/<?php $category = get_the_category(); echo $category[0]->cat_ID; ?>.jpg" /> 

<?php endif;?>

basically just change

echo $category[0]->cat_name;

to

echo $category[0]->cat_ID;

which will make your image 16.jpg or 3.jpg whatever corresponds with your category number.

share|improve this answer
    
I think many people prefer to use cat id as it's safer and allows you to change the category label or slug. –  Damien Jul 13 '12 at 17:13
add comment

I've successfully used Taxonomy Images by Michael Fields - it adds management functions to the category / taxonomy edit page, so that featured images work for taxonomies just as they do with posts/pages. Has all the functions you need for your theming.

snap of the custom taxonomy admin screen

share|improve this answer
add comment

I'm trying to implement this but my theme follows a different format.

$category[0]->cat_name;    
$default_thumb = $bloginfo.'/images/'.'$category->category_nicename'.'jpg';

I'm getting an error. The actual code for the above was:

$default_thumb = $bloginfo.'/images/post_thumb.jpg';

EDIT - Managed to get it to work.

$cat was already declared earlier. So I had to change the code to

$default_thumb = $bloginfo.'/images/'.$cats.'.png';

If someone could take a look at this and let me if this is the right way to code, i'd appreciate it.

Thanks.

share|improve this answer
1  
I managed to do it. Noticed that $cats was already declared in the function. So had to simply follow this format. –  Sledge81 Jan 14 '12 at 10:20
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.