Tutorial: Rotating Header Images

by Mike Nichols · 12 comments

Chris Pearson wrote several themes before he began working on Thesis. Two of the most popular were Cutline and Neoclassical. A feature these themes shared was rotating header images which changed each time the screen was refreshed, much like the Multimedia Box does now.

This article shows you a quick way to implement the Neoclassical- and Cutline-style rotating headers in Thesis. It is done entirely with a function in custom_functions.php. The header image is clickable. I wrote this function for Fred First’s blog, Fragments from Floyd when he switched from Neoclassical to Thesis. Another demonstration can be seen at my Thesis Demo site.

Here’s how to do it:

1. Make a new folder in /thesis/custom for the header images. Mine is called /header_images.

2. Put your header images into this folder. There is no limit to the number of images you can use. They should be named “header_1.jpg,” “header_2.jpg” etc, just like in Neoclassical and Cutline.

The images must all be of the same file type: .jpg, .gif, .png etc. So if you are using .gif files, the first header image would be named “header_1.gif” and so on. See Step 6 to set the image file type.

Make sure your header images are sized appropriately. If you are uncertain, see “Using the Layout Constructor to Build and Tweak a 1, 2, or 3-column Design” in the Thesis User’s Guide. At the bottom is information on the best header image sizes for 1-, 2-, and 3-column layouts. It is strongly recommended that all your header images be the same size — it would be jarring to the user to have the header jumping around at each page refresh!

3. Put the following code into your custom_functions.php file:

/* Rotating Header Images */
function rotating_header_images() {
/* Change the second number to the number of header images you are using. */
$random_image = rand(1,3);
?>
<!-- Change the width and height to your image size -->
<a href="<?php bloginfo('url'); ?>"><img src="<?php bloginfo(template_url)?>/custom/header_images/header_<?php echo $random_image;?>.gif" width="905" height="140" align="center" alt="Random header image... Refresh for more!" /></a>
<?php
}
add_action('thesis_hook_after_title', 'rotating_header_images');

4. Change the second number in the “$random_image” statement to the number of header images you are using. It is set to 3 in the example.

5. Change the file type if necessary. It is set to “.jpg” now, but it can be other types of images — but remember, they must all be the same!

6. Change the width and height values to that of your header images. If your images are presized to fit, you can delete these two values entirely.

7. Change the “alt” text if you wish.

This function inserts the header image after the blog title, just like in Neoclassical and Cutline. If you want to have rotating images instead of a blog title, use “thesis_hook_before_title” in the “add_action” statement (if your blog title is part of the image, for instance).

I hope this short guide was informative and easy to follow. You are invited to make a comment below. You also may email me by using the “Contact” tab in the button bar.

©2009 Michael L Nichols. All rights reserved.

What next?

Your comments are always welcome, and are important to this blog’s community! Leave a comment now, or read the comments.

You can find several related articles in the “Related Articles” list below. In the footer you will find a lists of Popular Posts, Recent Posts, and you may browse by Categories, or tags. There’s also a Google Custom Search box to help you find just what you want.

Get free updates by RSS or email!

If you have enjoyed this article, please consider subscribing to article updates, using an RSS reader, or by email. It’s free and is a great way to make sure you don’t miss a single article! I also invite you to follow me on Twitter!

Why not share this article with others!

Share this article with your friends using your favorite social media service, such as StumbleUpon, or Digg. Check out the icons below under “Share This Article With Others” for other social media, including del.icio.us, Technorati, Sphinn, Friendfeed, FaceBook, MySpace andLinkedIn! You can also email or print the article, and even tweet it using Twitter!

Share and Enjoy:
  • StumbleUpon
  • Digg
  • del.icio.us
  • Technorati
  • Facebook
  • MySpace
  • FriendFeed
  • Sphinn
  • LinkedIn
  • Twitter
  • email
  • Print
  • PDF
Related Articles

{ 12 comments… read them below or add one }

mari-lyn January 12, 2010 at 5:19 pm

HI MIke,
I have followed your instructions in doing the rotating header. somewhere there must be a place to insert the header images. since I am rather new at doing this, where do I insert? This is the error i got..

Warning: rand() expects exactly 2 parameters, 1 given in /home8/sedonapi/public_html/heartjam.com/wp-content/themes/thesis_16/custom/custom_functions.php on line 35
Random header image… Refresh for more!

Mari-Lyn

Reply

Barry January 14, 2010 at 9:14 pm

Mike,

Fantastically instructive website – thanks so much for your time, effort and brilliance!

As a newcomer to Thesis and coding, I’m probably getting in over my head but I’m tempted to dream: is there any way I could format my header vertical space to place two rotating images, one each left and right of the website’s logo header (static) image? Just a crazy idea but doable?

Barry

Reply

Beat Schindler January 16, 2010 at 1:54 pm

Hi, and thanks for sharing this gr8 stuff. Complete newbie, two q’s:
- how to make it clickable? (I know, basics. no pb with the header images or the rotating though)
- is each rotating header clickable individually?
Thanks a bunch!
Beat

Reply

Sandro Meyer January 17, 2010 at 11:12 am

Hey Mike,

Thanks for this Tutorial. I have a minor question here. Please have a look at my site:

http://www.lickmylife.ch

I would like to have a Rotating Header image, BUT with a Small Box next to it to show some ad’s or text in there. Just like on this Blog:

http://www.fourhourworkweek.com/blog/

How would I do that with Thesis/Wordpress?

thank you so much in advance. Keep up the good work
cheers,
Sandro

Reply

Mike Nichols January 17, 2010 at 12:13 pm

Dear readers,

I receive notice of all comments posted on the articles. I have chosen to answer many of these requests by email rather than here in the comments. I will recap my email answers briefly:

Mari-lyn:
The parameters for the random number are (1,x) where x is the number of images in your rotating header file. You appear to have changed the first number which is a no-no, resulting in the error message. You put your images in the header_images folder.

Barry:
You can create two boxes and place them on either side of the header. Then populate them with rotating images, either flash or by using my Rotating Image Gallery tutorial here on Thesis Theme Tools.

Reply

Mike Nichols January 17, 2010 at 12:15 pm

Beat,

The rotating header images are clickable, but they all will take you back to the home page. Unfortunately, they cannot have individual url’s.

Reply

Mike Nichols January 17, 2010 at 12:23 pm

Hi, Sandro,

The rotating header images were designed to take up the entire header image area. However, you might try the following — this is off the top of my head and may not work.

Define a box in custom.css the size of the header you desire. Make sure to include a float: left property. Wrap the rotating image in the css box’s div so that it occupies only that space. Make sure your header images are the size of the box you defined.

Then create another box for your ad and place it in the right side of the header area with a float:right property. You may have to change the add_action statement to place it in another hook, such as the thesis_hook_header.

Though I haven’t tested this, there’s no obvious reason why it wouldn’t work, given a bit of experimentation.

Let me know if this works for you!

Reply

Tyson January 26, 2010 at 10:07 pm

Hi, love this trick! Thanks for putting it together. The only issue I found after careful implementation is that the header images are right-aligned for some reason on my test site (http://yourmusicteachersite.com/). Just curious why this might be happening. Thx again!

Reply

Tyson January 27, 2010 at 2:13 am

Update: The only way I could get the images to go back to “center” was to reduce their width. (900px instead of 928px in a 2-column layout that totals 870px for content and sidebar). Not ideal, but works in the meantime!

Reply

LiLu February 10, 2010 at 12:32 pm

No matter what I do, I keep getting this error message in the header area of my blog:

“Parse error: syntax error, unexpected ‘}’ in /home8/livitluv/public_html/liluslayouts/wp-content/plugins/thesis-openhook/functions.php(275) : eval()’d code on line 9″

My code is as follows:

/* Rotating Header Images */
function rotating_header_images() {
/* Change the second number to the number of header images you are using. */
$random_image = rand(1,2);
?>

<a href="”><img src="/custom/header_images/header_.gif” width=”990″ height=”191″ align=”center” alt=”Random header image… Refresh for more!” />
<?php
}
add_action('thesis_hook_before_title', 'rotating_header_images');

I have 2 jpgs that are 990×191. Pretty simple… or it should be. No idea what could be causing this?

Reply

LiLu February 10, 2010 at 1:01 pm

Never mind- figured it out using the hooks instead. Thanks anyway!

Reply

David M February 11, 2010 at 7:01 am

Hi,
I would like to include rotating taglines on my blog. I would guess it’s a similar principle to rotating header images, but I can’t quite seem to work out how it’s done. I’d greatly appreciate it if you could advise me what to do, or tell me where to find out!

PS. I’m using Neoclassical, not Thesis, but I’d imagine the principle is the same.

Cheers

Reply

Leave a Comment

  • Visit My Other Site!