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. 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?
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
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?
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
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".
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.
<?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:
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?
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:
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?
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