treehouse : what would you like to learn today?
Web Design Web Development iOS Development

[Solved] Changing Body Background

  • Ok so I've got this bit of code in my header.php file:


    <body <?php if (function_exists('body_class')) body_class(); ?>>
    }

    Now on my home page, WP adds a class called "home" to my body tag, which i style like so:

    .home {
    background: url(images/morning.jpg) repeat-x #fff;

    What I'd now like to do is show two different body backgrounds at different times in the day. So between the hours of 6am and 5pm I'd like the body background to be (morning.jpg) and between 6pm and 5am I'd like to to display a different body background image (night.jpg).

    How would I go about doing something like this?

    Thanks in advance!
  • You could do something like this:

    <?php 

    $time = date(\"H\");

    if ($time >= 9 && $time < 18) { ?>
    <link rel=\"stylesheet\" href=\"../your/path/day.css\" type=\"text/css\" media=\"screen\" />
    <? }

    else { ?>

    <link rel=\"stylesheet\" href=\"../your/path/night.css\" type=\"text/css\" media=\"screen\" />
    <? }; ?>
  • "TheDoc" said:
    You could do something like this:

    <?php 

    $time = date(\"H\");

    if ($time >= 9 && $time < 18) { ?>
    <link rel=\"stylesheet\" href=\"../your/path/day.css\" type=\"text/css\" media=\"screen\" />
    <? }

    else { ?>

    <link rel=\"stylesheet\" href=\"../your/path/night.css\" type=\"text/css\" media=\"screen\" />
    <? }; ?>


    Where would I place that in my code?
  • That would be in your <head> section.
  • No luck. I'm not having any background image show at all. Any ideas?
  • Solved! Cheers TheDoc, and thanks for your patience.
  • No worries - glad you got it sorted!