elegant themes

Random Header Images without Javascript

| March 20, 2007 | 2 Comments

Many blogs use random header images in their header so that every time a page is reloaded, a new image appears in the header of the site. This adds interest and variation for readers.

To accomplish this, most sites use a combination of CSS and Javascript. Rob Griffith at The Robservatory relates how he set up his family’s site to have rotating header images without using any Javascript. His method involves

  • making a minor change to the CSS that calls the header image
  • creating a folder specifically for header images
  • naming the images sequentially (i.e. header1.jpg, header2.jpg)
  • and adding a few lines to header.php.

Random header images for WordPress>>

0saves
If you enjoyed this post, please consider leaving a comment or subscribing to the RSS feed to have future articles delivered to your feed reader.

Tags: ,

Category: Tips

Elegant WP Themes

Comments (2)

Trackback URL | Comments RSS Feed

  1. siddiq says:

    For the purposes of this example, we’re going to use 5 different images to pull off our random header effect. It’s important that these images be the same size — this will keep your image declarations consistent and W3C-valid. So, grab the five header images of your choice, and name them according to the following convention:

    header_1.jpg
    header_2.jpg
    header_3.jpg
    header_4.jpg
    header_5.jpg

     
    The code is
     
    <img src=”http://path_to_images/header_<?php echo(rand(1,5)); ?>.jpg” width=”image_width” height=”image_height” alt=”image_alt_text” />

  2. nick says:

    Thanks siddiq that’s a real useful piece of code!

Leave a Reply




If you want a picture to show with your comment, go get a Gravatar.

More in Tips (73 of 83 articles)