When I started my Tales series, I wanted it to have a distinct category and RSS feed so that those posts wouldn’t get mixed in with the tech comm–related posts. I also wanted to have a header image for any posts in the Tales category that was different than the standard one.

Here’s the code that I put together to make the header image conditional based on the category, with some substitutes put in so you can change things based on your needs). Add this code to header.php:

<div id="header">
<?php if (in_category('1')) { ?>
<img src="http://www.mysite.com/images/header1.jpg">
<?php } elseif (is_category('Category Name')) { ?>
<img src="http://www.mysite.com/images/header1.jpg">
<?php } else { ?>
<img src="http://www.mysite.com/images/header2.jpg">
<?php } ?>
</div>

Notice two things:

  • This code is set up for two conditions to show header1.jpg. You can swap out the category number or name for yours. (To see a category’s number, in your WordPress console’s Categories screen, mouse over the name and look for the tag ID in the URL that appears in your status bar.) You could also add as many different conditions as you like.
  • This works with an image placed directly in the div. I don’t know enough PHP to know if I can have it change a background image in the CSS.