Confessions of a Six Figure Professional Blogger

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.

If you enjoyed this article, you might also like...

  1. Using Gravatars On Your Blog – Why And How
  2. Insiders: An Opt-In Technique Worth Testing. I Will Be. :-)
  3. How To Use a Custom Page Template In Wordpress
  4. Robots.Txt – The Often Forgotten File
  5. How To: Putting the Full Blog Post Into An Aweber Blog Broadcast
Six Figure Blogger Blueprint

Get your copy of the Six Figure Blogger Blueprint.

You'll Discover...

  • 3 vital questions to pick the right niche.
  • The layout for funneling traffic into your blog.
  • How this blog generates thousands of dollars monthly - without a single banner ad.

Your information will not be shared with anybody - ever.

  • 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.
  • Always wondered how that worked, thanks!
  • Great. Easy and effective solution.
  • 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!
  • 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
  • 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.
  • This is great! Thanks for sharing!
  • Great. Another blogger who I've noticed using that is Yaro Starak.
  • your header look great.. :)
    i learn this 'hack' through pearsonified.com.
blog comments powered by Disqus