PHP变量内部HTML样式标记

时间:2017-04-28 06:18:10

标签: php html css wordpress

我在PHP 5.3.29网站上使用WordPress 4.7.4

我想根据一天中的时间更改背景。 我发现了这段代码here

我有php文件header.php

在这个文件中使用我正在做的html代码:

的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>

我没有看到$ img_name变量传递到此样式标记中 http://website.com/wp-content/uploads/2017/04/

我认为它类似于这篇文章 php variable inside echo 'html code'

我在没有运气的情况下使用了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_;

1 个答案:

答案 0 :(得分:0)

body.custom-background{background-image:url('http://aarondc.com/wp-content/uploads/2017/04/"$img_name"');} 中使用双引号而不是单引号

<SSOService><IdentityProviderURL>https://localhost:9443/samlsso</IdentityProviderURL>