2

I'm using PHP 5.3.29 on a WordPress 4.7.4 Site.

I want to change backgrounds based on the time of day. I found this this code here

I have php file header.php

In this file to use html code I am doing:

header.php:

<?php
    $time = date("H");    
    if( $time >= 06 && $time < 10 )
        $img_name = 'sunrise.jpg';
    if( $time >= 10 && $time < 17 )
        $img_name = 'day.jpg';
    if( $time >= 17 && $time < 19 )
        $img_name = 'sunset.jpg';
    if( $time >= 19 && $time < 6 )
        $img_name = 'night.jpg';
?>

<style>
    body.custom-background{background-image:url('http://website.com/wp-content/uploads/2017/04/<?php echo $img_name;?>');}
</style>

I'm not seeing the $img_name variable passed into this style tag http://website.com/wp-content/uploads/2017/04/

I think its similar to this post php variable inside echo 'html code'

I took a stab at using Heredocs with no luck:

testing using Heredocs

<?php
    $time = date("H");    
    if( $time >= 06 && $time < 10 )
        $img_name = 'sunrise.jpg';
    if( $time >= 10 && $time < 17 )
        $img_name = 'day.jpg';
    if( $time >= 17 && $time < 19 )
        $img_name = 'sunset.jpg';
    if( $time >= 19 && $time < 6 )
        $img_name = 'night.jpg';
?>
echo <<<_EOI_
<style>
    body.custom-background{background-image:url('http://website.com/wp-content/uploads/2017/04/"$img_name"');}
</style>
_EOI_;
3
  • 1
    use double quotes instead of single in url() Commented Apr 28, 2017 at 6:20
  • Alive to Die: THANK YOU!!! I could have sworn I tried that value with double quotes! You're awesome!!!! Commented Apr 28, 2017 at 6:24
  • use ${$img_name} instead of "$img_name" and you are also closing the php tag before echo Commented Apr 28, 2017 at 6:27

1 Answer 1

0

Use double quotes instead of single in url() and it will fine

body.custom-background{background-image:url('http://aarondc.com/wp-content/uploads/2017/04/"$img_name"');}
Sign up to request clarification or add additional context in comments.

1 Comment

Thank you again! This is my first time having a question answered.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.