2015-10-22 211 views
-1

我使用下面的代码在我的header.php文件有根据页面上的不同DIV:WordPress的:回声里面的PHP回声?

<?php 
if (is_singular('movie')) { 
    echo "<div class='heroImagePage' style='background-image:url(blur.jpg);'>"; 
} elseif (has_post_thumbnail($post->ID)) { 
    echo "<?php $image = wp_get_attachment_image_src(get_post_thumbnail_id($post->ID), 'full'); ?> <div class='heroImage' style='background-image:url(<?php echo $image[0]; ?>);'>"; 
} else { 
    echo "<div class='heroImagePage' style='background-image:url(blur.jpg);'>"; 
} 
?> 

if (is_singular('movie')) {else {做工精细的条件,但是,检查是否有一个有特色的一个图像不。其中的代码是获取特色图片的URL,以便它可以将其作为div的背景图片插入。我认为问题是回声里面的回声?

当我看着它在我的浏览器输出的代码,它看起来像这样:

<div class="heroImage" style="background-image:url(<?php echo ; ?>);"> 

我试过和网上看了帮助解决问题,但似乎无法找到解决的办法。感谢任何帮助。

+2

这不是PHP的工作原理。尝试从这里开始阅读一下PHP语法:http://php.net/manual/en/langref.php – Mike

回答

2

PHP不会在字符串内执行。简单地分配$image变量之前呼应串

elseif (has_post_thumbnail($post->ID)) { 
    $image = wp_get_attachment_image_src(
     get_post_thumbnail_id($post->ID), 'full'); 
    echo '<div class="heroImage" style="background-image:url(', $image[0], ')">'; 
} 
+0

谢谢!这工作。 – morart

-1

尝试呼应字符串之前分配$image变量。
I.e.

<?php 
if (is_singular('movie')) { 
    echo "<div class='heroImagePage' style='background-image:url(blur.jpg);'>"; 
} elseif (has_post_thumbnail($post->ID)) { 
    $image = wp_get_attachment_image_src(get_post_thumbnail_id($post->ID), 'full'); 
    echo "<div class='heroImage' style='background-image:url" . $image[0] . ')">' ; 
} else { 
    echo "<div class='heroImagePage' style='background-image:url(blur.jpg);'>"; 
} 
?> 
+0

为什么这个答案是-2,接受的答案是+2,但他们都说完全一样的东西,几乎一字一句? – Mike

+0

不知道,当我回答没有其他答案的时候,我有了一个赞成,然后结果-3! –