How To Have Random, Rotating Images In Your Blog Header

As part of my new header design for this blog, I am using a series of 12 header images which rotate randomly.

I’ve had a few people comment on that and wonder how I did it. It is actually VERY easy to do.

First, you create the images you want in your header. Make them all the same dimensions that way they can change without making your layout different.

When you name the images, give each of them a numeric file name. For example:

  • topphoto_1.jpg
  • topphoto_2.jpg
  • topphoto_3.jpg
  • and so on.

Make sure not to skip any numbers. It has to be a constant series of numbers.

Now, you modify your blog theme. In the section of your header.php file where you want this image to appear, use this instead:

<img src=”topphoto_<?php echo rand(1,X); ?>.jpg” width=”123” height=”123” />

Now, the most important part of that is the PHP code inside of the filename. What you’re doing is telling PHP to give you a random number between 1 and X. Replace X with the number of images you have available. In my case, I have 12 header images, so I would use rand(1,12). Also, make sure you use the actual height and width of your images in the HTML.

That’s all there is to it, guys. No javascript necessary. No plug-ins, either.

Blog Transformation Challenge

Like what you read?

... then take the next step. Register for the 30 Day Blog Transformation Challenge. TRANSFORM your blog by perfecting your writing, optimizing your design, and learning how to optimize the marketing funnel.
Click to Learn More ... or...

  • http://www.izzataziz.net/ Izzat Aziz

    your header look great.. :)
    i learn this ‘hack’ through pearsonified.com.

  • http://www.melvinblog.com/ Melvin

    Great. Another blogger who I've noticed using that is Yaro Starak.

  • http://leadthewayout.com/ Patrice

    This is great! Thanks for sharing!

  • http://2009Taxes.org/ Steve

    Great tip. A little php custom code does the trick. I try to refrain from modifying the themes too much so when the theme updates I don't have to worry about wiping out modifications.

  • cadilacjax

    David does this only work for Wp or can I script it in blogger? Just curious as it sounds and looks interesting. Would like to give it a go. Thanks and good luck with the new one, wake his butt up and tell him he has work to do… LOL

  • http://twitter.com/banjopaul67 Paul Pope Sr.

    Hey Dave, thanks so much for this tip. As a Blog Masters Club student, I have always valued your knowledge and I learn from you. You are a big help and Iove this tip!

  • http://agentdeepak.com/ Agent Deepak

    Great. Easy and effective solution.

  • http://funny-tshirt.net/ Leslie Bergstrom

    Always wondered how that worked, thanks!

  • http://twitter.com/ChaseYourLion Heath Mullikin

    Quick question. Which file do you upload your images to? I've got the images and code ready. Just not sure where the code is pulling them from. Thanks.

  • infopediaonlinehere

    nice post..

  • Jay

    Is there any way to have those images rotate randomly while still on the page WITHOUT having to refresh?

  • Janine

    Thanks so much for sharing – so easy to implement!