简体   繁体   English

PHP变量内部HTML样式标记

[英]PHP Variable Inside HTML style tag

I'm using PHP 5.3.29 on a WordPress 4.7.4 Site. 我在WordPress 4.7.4网站上使用PHP 5.3.29

I want to change backgrounds based on the time of day. 我想根据一天中的时间更改背景。 I found this this code here 我在这里找到了这个代码

I have php file header.php 我有php文件header.php

In this file to use html code I am doing: 在这个文件中使用我正在做的html代码:

header.php: 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/ 我没有看到传递给此样式标记的$ img_name变量http://website.com/wp-content/uploads/2017/04/

I think its similar to this post php variable inside echo 'html code' 我认为它类似于回传'html代码'中的这个帖子php变量

I took a stab at using Heredocs with no luck: 我毫不费力地使用了Heredocs:

testing using Heredocs 使用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_;

url()使用双引号而不是单引号,它会很好

body.custom-background{background-image:url('http://aarondc.com/wp-content/uploads/2017/04/"$img_name"');}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM