WordPress Development Stack Exchange is a question and answer site for WordPress developers and administrators. Join them; it only takes a minute:

Sign up
Here's how it works:
  1. Anybody can ask a question
  2. Anybody can answer
  3. The best answers are voted up and rise to the top

So OK....I might be way of but here goes. Im using a plugin that gives the post a Featured Image if its not set. So Im trying to make it that depending on in which category the post is in the Featured Image should correspond to the category.

So this is the code om trying to mend.

<?php get_the_image( array( 'meta_key' => 'feature_img', 'size' => 'medium', 'width' => '300', 'height' => '170', 'image_class' => 'feature', 'default_image' => 'http://www...../uploads/img_cat_' . $category->cat_ID . '.jpg' ) ); ?>

Its the " ' . $category->cat_ID . ' " code Im not sure about .

I want the JPG displayed img_cat_4.jpg if the post belongs to Cat nr 4 etc.

Any takers?

share|improve this question
posts can have several categories; are you OK with the first category (probably the one with the lowest ID) ? – Michael Nov 21 '12 at 20:06

I have not used that plug-in but I would guess that your function get_the_image() doesn't accept that parameter as part of the array.

I would first check for that custom function to be active. If you turn that plug-in off you will get an error on your page:

<?php if ( function_exists( 'get_the_image' ) ) { get_the_image(); } ?>

Then just insert your parameters as above.

You can try this (untested) is this in the loop? if not this might not work for you.

$category = get_the_category(); 
if ( function_exists( 'get_the_image' ) ) get_the_image( array( 'meta_key' => 'feature_img', 'size' => 'medium', 'width' => '300', 'height' => '170', 'image_class' => 'feature', 'default_image' => 'http://www...../uploads/img_cat_' . $category[0]->cat_name . '.jpg' ) ); 
share|improve this answer
Wow....that latest actually worked...!!!! Thank you so much. At first it seemed wrong but instead of putting in the Cat Number it gave the category name...so I just need to change the pictures from cat_5 too cat_Zxy. Incredible! Thanks again! – Klint Nov 21 '12 at 20:32
Glad to help, But Michael is right it will only return the first category. If you need to check for others it will need to be done differently. – Ken Nov 21 '12 at 20:34
Yes Michael was right regarding the fact that it returns the first Category. But its a start so another big thank you Michael and Ken. – Klint Nov 21 '12 at 20:41

Your Answer


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.