Is there a convenient and configurable way to alternate between different header images?

The main features I'm looking for are:

  • Random select from a set of images
  • Specific header image in given date range (a la Google Doodle)
  • Specific header image for specific posts or pages

You can try this

Random from a set of images :

<img src="http://www.mywebsite.com/images/image_<?php echo rand(1,10); ?>.jpg" alt="" />

This would load a random image from image_1.jpg, image_2.jpg ... to image_10.jpg


$images = array("cool_image.jpg", "nice_pic.jpg", "sunset.jpg");
$rand = array_rand($images);

<img src="http://www.mywebsite.com/images/<?php echo $images[$rand]; ?>" alt="" />

This would select a randow image from thoses specified in the $images array.

Date range :

if ((date('m') == 3) && (date('d') == 17)) { ?>
    <img src="http://www.mywebsite.com/images/stpatrick.jpg" alt="" />
<?php } ?>

This would show an image for St Patricks

Specific posts or pages :

See : Change image based on menu item id

  • Excellent, thanks. These lines should replace the existing one in header.php, right?
    – Adam Matan
    Jun 11 '11 at 17:03
  • @Adam Matan, If there is already a <img /> in your header.php then yes that's the one you would replace.
    – mike23
    Jun 11 '11 at 17:28

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.