Skip to content


WordPress: heads up

Another weekend, another WordPress theme tip. This time I was keen on creating a random header image for one of my blogs so that the reader gets a new image every time a page is refreshed. It’s not exactly a critical feature but it does add a little pizazz to a blog.

Like many things in WordPress, there are several ways to accomplish this.

Matt Mullenweg, WordPress’ founding developer, has one solution. The code looks so scarily complicated I wouldn’t be at all surprised if you ruptured the spacetime continuum if you made a typo.

Chris Pearson, a WordPress theme developer, has another solution. It’s simple and elegant, and makes use of the PHP rand function to generate a random number.

The code to drop in the header.php file of your WordPress theme is:
<img src="< ?php bloginfo('template_url'); ?/>/images/logo_< ?php echo(rand(1,4)); ?>.png" alt="< ?php bloginfo('name'); ?>" />

In layman’s terms, that tells WordPress to look into the “images” subfolder of the current theme folder for a PNG file named logo_x (where x equals a random number between 1 and 4). You would, of course, have to have PNG files named logo_1.png, logo_2.png, logo_3.png and logo_4.png in the “images” subfolder.

Posted in WordPress.


0 Responses

Stay in touch with the conversation, subscribe to the RSS feed for comments on this post.



Some HTML is OK

or, reply to this post via trackback.