0

Does anyone have a clever idea to get a random/changing background for just the widget sidebar?

  • Is this a theme you can edit? – s_ha_dum Mar 24 '14 at 15:03
  • Yes totally editable them. I looked around for a plugin and found some nice ones that do full page backgrounds, but don't specify areas. I don't have the time/budget to have something special written. – ERM Mar 24 '14 at 15:57
  • Where are you pulling the images? Do you have a set of images you already know are going to be part of the randomization or are all the images user uploaded? – Howdy_McGee Mar 24 '14 at 21:44
1

Place the following code above the DOCTYPE in header.php:

<?php
  $bg = array('bg-01.jpg', 'bg-02.jpg', 'bg-03.jpg', 'bg-04.jpg', 'bg-05.jpg', 'bg-06.jpg', 'bg-07.jpg' ); // array of filenames

  $i = rand(0, count($bg)-1); // generate random number size of the array
  $selectedBg = "$bg[$i]"; // set variable equal to which random filename was chosen
?>

Now place this code within <head> in header.php

<style type="text/css">
body{
background: url(<?php echo bloginfo('stylesheet_directory');?>/images/<?php echo $selectedBg; ?>) no-repeat;
}
</style>

The path <?php echo bloginfo('stylesheet_directory');?>/images/indicates that your random images will be stored within your theme under the directory "/images".

The original article on this technique can be found at CSS tricks.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.