1. Advertising
    y u no do it?

    Advertising (learn more)

    Advertise virtually anything here, with CPM banner ads, CPM email ads and CPC contextual links. You can target relevant areas of the site and show ads based on geographical location of the user if you wish.

    Starts at just $1 per CPM or $0.10 per CPC.

Wordpress, how to insert specific images for specific categories (not in posts)

Discussion in 'WordPress' started by pipes, May 29, 2008.

  1. #1
    Hello, for a new theme i have in mind, i would like to insert a photo image into a specific area of the layout, and i want a different (not random) image to display in the area that will be set for that category.

    For example i will have a top right section of the theme where the images would go, say 200x200 px images.
    SEMrush
    So if someone is in the green trees category (just an example) the image showing in the top-right of the theme would be a photo of a green tree.

    This would not be in posts, i dont need to insert these images into my posts.

    Do you know what i mean?

    I know there is a plugin to insert images next to your categories, but i need it to work slightly different from that.

    Any ideas how i may achieve the above?
     
    pipes, May 29, 2008 IP
    SEMrush
  2. tmeyer45458

    tmeyer45458 Peon

    Messages:
    355
    Likes Received:
    45
    Best Answers:
    0
    Trophy Points:
    0
    #2
    Hey Pipes -

    Try this...I "think" it's what you're looking for:

    <?php if (is_category('Category A')) { ?>
    <img src="your-image.jpg" />

    <?php } elseif (is_category('Category B')) { ?>
    <img src="your-image.jpg" />

    <?php } else { ?>
    <img src="your-image.jpg" />
    <?php } ?>

    Downside is that everytime you add a category, you will have to update the code above. As for plugins...I'm not sure. BTW: Great Avatar;)
     
    tmeyer45458, May 29, 2008 IP
  3. pipes

    pipes Prominent Member

    Messages:
    12,767
    Likes Received:
    958
    Best Answers:
    0
    Trophy Points:
    360
    #3
    Hi tmeyer, thanks very much for providing the code, i dont mind having to update the code each time a category is added, its a small price to pay :)

    Regarding Category A, Category B etc, im guessing i would replace these with the category names?
     
    pipes, May 30, 2008 IP
  4. tmeyer45458

    tmeyer45458 Peon

    Messages:
    355
    Likes Received:
    45
    Best Answers:
    0
    Trophy Points:
    0
    #4
    Yep. It would be the "Category Slug". Usually the name of the category (small caps) unless you maually change it. That's assuming you are using seo friendly urls. Otherwise it would be something like cat12, cat13, cat15
     
    tmeyer45458, May 30, 2008 IP
    pipes likes this.
  5. pipes

    pipes Prominent Member

    Messages:
    12,767
    Likes Received:
    958
    Best Answers:
    0
    Trophy Points:
    360
    #5
    Thanks very much tmeyer, il give it a try as soon as ive got some other work out the way.
     
    pipes, May 30, 2008 IP
  6. a!!!!1

    a!!!!1 Peon

    Messages:
    346
    Likes Received:
    1
    Best Answers:
    0
    Trophy Points:
    0
    #6
    i tried this and it didnt work
     
    a!!!!1, Nov 15, 2008 IP
  7. cuhela

    cuhela Peon

    Messages:
    6
    Likes Received:
    0
    Best Answers:
    0
    Trophy Points:
    0
    #7
    Can you try this codes ?

    <?php
    $category = get_the_category();
    $catname = $category[0]->cat_name;
    if($catname != "") {
    echo "<img src=\"".$catname.".jpg\" />";
    }
    else {
    echo "<img src=\"nocategory.jpg\" />";
    }
    ?>
    PHP:
    Your images must be, "your category name.jpg". :)
     
    cuhela, Nov 15, 2008 IP
  8. a!!!!1

    a!!!!1 Peon

    Messages:
    346
    Likes Received:
    1
    Best Answers:
    0
    Trophy Points:
    0
    #8
    What if you don't want to just display images, you want to show a different set of html based on category? for example if category=1 then show code#1, and if category=2 then show code#2, and if category=else then show nothing. :confused:
     
    a!!!!1, Nov 15, 2008 IP
  9. cuhela

    cuhela Peon

    Messages:
    6
    Likes Received:
    0
    Best Answers:
    0
    Trophy Points:
    0
    #9
    <?php
    $category = get_the_category();
    $catid = $category[0]->cat_ID;
    if($catid != "") {
      if($catid = 1) { include($catid.php); // if cat 1 -> 1.php }
      if($catid = 2) { include($catid.php); // if cat 2 -> 2.php }
    }
    else {
    echo "the other codes goes here";
    }
    ?>
    PHP:
    if you dont want to include files, you write your codes directly.
    For example:

    if($catid != "") {
      if($catid = 1) { echo "your codes for category-$catid"; }
    }
    PHP:
    It seems like this:
    your codes for category-1
    HTML:
     
    cuhela, Nov 16, 2008 IP
  10. a!!!!1

    a!!!!1 Peon

    Messages:
    346
    Likes Received:
    1
    Best Answers:
    0
    Trophy Points:
    0
    #10
    So the first PHP you gave is the main page, and that's where you're setting up all the "if category=x then go to the next php" and the next PHP you gave would be the 1.php, 2.php, etc for each specific category? Is that right? and what's the 3rd one?
     
    a!!!!1, Nov 16, 2008 IP
  11. cuhela

    cuhela Peon

    Messages:
    6
    Likes Received:
    0
    Best Answers:
    0
    Trophy Points:
    0
    #11
    Ooops :) I'm sorry..
    Use this code:
    <?php
    $category = get_the_category();
    $catid = $category[0]->cat_ID;
    if($catid != "") {
      include($catid.php);
    }
    else {
    echo "the other codes goes here";
    }
    ?>
    PHP:
     
    cuhela, Nov 16, 2008 IP
  12. a!!!!1

    a!!!!1 Peon

    Messages:
    346
    Likes Received:
    1
    Best Answers:
    0
    Trophy Points:
    0
    #12
    So would I change the $category[0] or what? What's the part about $catid.php, that's where I'd make a new php file for each category and include the ad code in there?
     
    a!!!!1, Nov 16, 2008 IP
  13. geekpriyan

    geekpriyan Peon

    Messages:
    126
    Likes Received:
    1
    Best Answers:
    0
    Trophy Points:
    0
    #13
    The answer said by tmeyer45458 was the correct one. I think you might have done any simple mistake in placing the Image files. If you can send me the details of your Wordpress site in PM. I will do it with pleasure :)
     
    geekpriyan, Nov 18, 2008 IP