Quick PHP wordpress help required. PLEASE!!!

Discussion in 'PHP' started by Taraffic, Aug 10, 2009.

  1. #1
    Hi,
    Im looking for a way to check the category and display a thumbnail based on the category. (this will be on the main page).
    The code i have so far just pulls in an image if no thumbnail is set. See below.

    Can anyone suggest a way to help me change it to select an image based on categories.
    e.g - If category has tumbnail, display it. Else If category is Sheep - Display sheep.jpg.

    Thanks.

    <?php
    $thumb = get_post_meta($post->ID, 'Image', true);
    if ($thumb == '')
    { ?>
    
    <a href="<?php the_permalink() ?>">
    <img src="http://www.mysite.com/images/standard.jpg" 
    alt="<?php the_title(); ?>" class="left" width="150px" height="150px"/>
    </a>
    
    
    <?php } ?>
    Code (markup):
     
    Taraffic, Aug 10, 2009 IP
  2. astrazone

    astrazone Member

    Messages:
    358
    Likes Received:
    5
    Best Answers:
    0
    Trophy Points:
    33
    #2
    how much categories are there?
     
    Last edited: Aug 10, 2009
    astrazone, Aug 10, 2009 IP
  3. wd_2k6

    wd_2k6 Peon

    Messages:
    1,740
    Likes Received:
    54
    Best Answers:
    0
    Trophy Points:
    0
    #3
    
    if (in_category('2')){
    //Display thumbnail for cat 2
    }
    else if(in_category('5')){
    //Display thumbnail for cat 5
    }
    
    PHP:
    and change cat numbers as required
     
    wd_2k6, Aug 10, 2009 IP
    agilius likes this.
  4. agilius

    agilius Well-Known Member

    Messages:
    1,021
    Likes Received:
    10
    Best Answers:
    0
    Trophy Points:
    100
    #4
    Like wd_2k6 said, that should do it. However if you have more categories, you should simply try a different method, like maybe adding a new custom field thumb_type or something, so you won't have to get to a lot of ifs and elses.

    Good luck :)
     
    agilius, Aug 10, 2009 IP