Imagination is the beginning of creation. You imagine what you desire, you will what you imagine and at last you create what you will.

George Bernard Shaw  (1856 - 1950)

 

Portfolio

From The Design Lab

Tweet

Tweet us...!
 

 
 

 
You are here :
Gajaparan.com
» Tutorials, Wordpress »
Random…
 


Random Background for your WordPress theme

In this tutorial I’m going to show you a simple trick to change background using ‘PHP RAND’. So, every time you reload your page you will get different background… ok… let’s do it…


For this tutorial I’m going to use the WordPress Default Theme.

First, make some small images with different colors and save them into the image folder inside your theme. And make sure you add a number from 0 – 9. [In this case I made a folder call – bg – inside images folder & name the images as – image_0, image_1 …. ]

Then, use this snippet to generate random numbers. [You can add this snippet to your function.php file or in header.php file]

function genRandomString() {
    $length = 1;
    $characters = '0123456789';
    $string = "";
    for ($p = 0; $p < $length; $p++) {
        $string .= $characters[mt_rand(0, strlen($characters))];
    }
    return $string;
}

After that, add this line of CSS code in your CSS file or in header.php file. [I’m going to add it to header.php file]

body {
	background: #d5d6d7 url('<?php bloginfo('template_url'); ?>/images/bg/image_<?php echo genRandomString(); ?>.jpg') !important;
}

final code looks like this...

That’s it… go back to the browser and hit reload…!!

 

Result


Hope you like this tutorial…!! Leave a comment if you have any questions… thank you.

 

PHP Reference : http://www.lost-in-code.com/programming/php-code/php-random-string-with-numbers-and-letters/

Share it with Others

If you have enjoyed this entry. Please feel free to Bookmark / Share it using one of the following social sites.
Short Url : http://goo.gl/B8sYY

Leave a Reply


Name Required:

Email Required:

Website Optional

Comment Required:

Please note: Comment moderation is enabled and may delay your comment. There is no need to resubmit your comment.

 
 

Archives


 

Categories