by ShibaShake

Ever seen those spiffy WordPress blogs with automatic changing headers? Some people dislike the effect because they find the animation to be too distracting.

I think this slide-show type, rotating header effect can be interesting in certain circumstances. This blog page, for example, has a demo rotating header. I have set the speed of rotation to be about 5 seconds so that you can see the changes occur.

In general, however, you want to set a much longer rotate period so that the animation does not happen too often and does not become too distracting to your readers.

The rotating header effect is actually not too difficult to achieve with JavaScript and the jQuery library.

1. Include the jQuery library

Add the code below to the functions.php file of your theme. Better yet, you may want to create a child theme, and include it in your child functions.php file. By using a child-theme, all this code will automatically transfer even when you make theme updates.

1
2
3
4
5
6
7
8
<?php
	add_action('init', init_method);
 
 
function init_method() {
	wp_enqueue_script('jquery');	
}    
?>

2. Rotate header function using the jQuery library

The example rotateHeader Javascript function below changes the header image of the Shiba WordPress theme. Just include it in your functions.php file.

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
<script type="text/javascript">
<!--
counter = 1; 
num_images = 9;
dir = "http://www.shibashake.com/wordpress-theme/images/slides/Tattoo/Header";
 
function rotateHeader() {
 
	var header_img = 'url(' + dir + '/image' + counter + '.jpg)';
 
	jQuery('#header').css('background-image', header_img);		
	counter++; if (counter > num_images) counter = 1;
}
//-->
</script>

Lines 3 to 5 – Sets up some global (static) variables for our rotateHeader function.

  • counter specifies the current image being shown.
  • num_images specifies the total number of images you want to rotate through.
  • dir is the URL directory of the header images you want to use. We assume that the images in the directory are named image1.jpg, image2.jpg, and so on.

Line 11 – Uses jQuery to reset the header background image. To use the code for your own theme, you must identify the container id for your theme header image, and replace #header name with your own theme header id.

3. Calling the rotateHeader function

Now all that is left is to call the rotateHeader function after a certain set interval of time. You can do this using the Javascript setInterval function.

<script type="text/javascript">
<!--
setInterval( "rotateHeader()", 5000 );
//-->
</script>

Include the setInterval command at the beginning to any post or page whose header you want to rotate. If you want all pages and posts to rotate, you can include it in your functions.php file.

This setInterval command causes the header background image to change every 5 seconds.

The first argument is some Javascript code which gets executed every set interval. For the sake of code readability, this is usually the name of a function which encapsulates all the code you want to execute.

The second argument is a time interval in milliseconds. 5000 milliseconds = 5 seconds.

You are done!

Adding this JavaScript code will give you a rotating WordPress header.

4. Pre-load rotating header images

A problem with just using the code above, is that there may be long delays every time a new header image needs to be loaded. This may result in a blank header, while a new image is loading.

To get the best effect, you want to pre-load all the header images before starting with the image rotations. Here is an example of how you can pre-load images in an object oriented fashion using Javascript.

Here is a cleaner and simpler way to pre-load images by just adding HTML code.

Just add your Javascript setInterval function to the onload event so that you will only start to rotate your header after all the images have finished loading.

Related Articles

<< Previous Next >>

<a href="http://shibashake.com/wordpress-theme/blog-background-effects" target="_top">Blog Background Effects</a>

Blog Background Effects

There are two interesting blog background effects that we will consider here - 1. A hard edge effect. 2. A soft edge effect. Both these blog background effects can be achieved with a simple PNG gradient background. PNG will allow you to create a transparent gradient that you can then apply to any background color. The Hard Edge Blog...

<< Previous Next >>

<a href="http://shibashake.com/wordpress-theme/ice-lady-wordpress-theme-images" target="_top">Ice Lady WordPress Theme Images</a>

Ice Lady WordPress Theme Images

This is my first theme image post using the Shiba Gallery Plugin. The gallery plugin allows me to easily show my theme images using a variety of Javascript galleries. In this case, I am using the Slide Viewer Javascript library. You can manually scroll through the different theme images below. Hover your mouse on the middle of the image and click...

<< Previous Next >>

<a href="http://shibashake.com/wordpress-theme/wordpress-search-widget-how-to-style-it" target="_top">Wordpress Search Widget - How to Style It</a>

Wordpress Search Widget - How to Style It

WordPress widgets offer us a lot of flexibility. By using widgets, we can easily include a variety of functions into different widget areas within our WordPress blog. However, it can sometimes be difficult to figure out how to properly style these widgets. Here we examine how to flexibly style the native WordPress Search Widget. This is the search...

<Playback Stop Play >

5 Comments

  1. Hi Eric,
    Try including your code in pre tags -

    <pre>
    Code here
    </pre>
    

    Do you use any echo functions for debugging? If the echo happens before the headers are sent, then it gets interpreted as the header, which will then cause the error you describe.

    8:31 am on March 4th, 2010 Reply
  2. Ooops! My code didn’t work in the post. Let me know and I can email it to you.

    7:23 pm on March 3rd, 2010 Reply
  3. Very nicely stated. This works wonderfully for me.

    The only issue I am getting is that the Theme I am using keep giving me an error every time I save one of the theme files I edit using the editor. The error is :

    Warning: Cannot modify header information – headers already sent by (output started at /home/#######/public_html/siamcc-org/wp-content/themes/siamcc/functions.php:6) in /home/#######/public_html/siamcc-org/wp-admin/theme-editor.php on line 75

    I have put these lines in the functions.php:

    num_images) counter = 1;
    }
    //–>

    I get the feeling it is the add_action function. I have tried to remove it but obviously it breaks the image rotation. Any suggestions?

    7:22 pm on March 3rd, 2010 Reply
    • I have exactly the same issue. Did u ever get the resolution? Thanks

      3:57 pm on May 16th, 2010 Reply
  4. Love the rotating header. Going to try this code tomorrow on a friends site. Send me word it there are any new ideas for making it better. I’d like to see how much can be done with the concept.

    9:00 pm on January 21st, 2010 Reply

RSS feed for comments on this post. TrackBack URL

Leave a Reply

search button search button
rss