0

I want to assign an image to 1 category and want the rest of the categories to be hidden on my home page. I tried this:

foreach((get_the_category()) as $category) {
    echo '<img src="http://organeek.net/wp-content/uploads/2014/10/85.jpg' . $category->cat_ID . '.jpg" alt="' . $category->cat_name . '" />';
}

It does not work for me because it shows all the images on all categories. I want to be able to choose only 1 or maybe 2 categories.

Thanks in advance for the help!

  • Please format your code properly. – Nilambar Oct 27 '14 at 3:24
0

It seems that you need to do this inside a post. For this, you can either use has_category() or in_category to test if a post belongs to the given category, and if the condition is true, apply your changes

Example:

if( has_category('name or I'd or slug of category to test) ) {
     echo '<img src="http://organeek.net/wp-content/uploads/2014/10/85.jpg' . $category->cat_ID . '.jpg" alt="' . $category->cat_name . '" />';
}else{
    //displays nothing if the post does not belongs to this category
}
  • Sorry, can you complete the code? Im afraid my coding skill is nowhere near advance. In fact, I'm a total newbie. :( – kira Oct 29 '14 at 8:17
  • See my updated code – Pieter Goosen Oct 29 '14 at 8:19
  • Just replace your code with mine – Pieter Goosen Oct 29 '14 at 8:21
  • Thanks for your prompt response. I seem to be doing something wrong with it as it's not working. Do I need to change anything in it? Thanks Pieter. I apologize if I'm my question is confusing you. Appreciate your help! – kira Nov 1 '14 at 12:22
  • Where exactly are you using this. Please file an edit to your question and add your current code and details on what exactly do you want to achieve – Pieter Goosen Nov 1 '14 at 14:35

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

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