PHP条件回显文本而不是HTML

时间:2016-02-09 05:08:08

标签: php html wordpress function

我试图在Wordpress中为主题创建一个条件, 如果元存在显示这个...其他显示。

<?php
    $scene_trailer = get_post_meta($post->ID, 'scene_trailer', true);
    if ( $scene_trailer ) {
        echo htmlentities('<div style="width: 645px; height: 364px; overflow: hidden">
            <iframe src="http://tvguide.com/<?php $key="scene_number"; echo get_post_meta($post->ID, $key, true); ?>/" width="645" height="430" scrolling="no" frameborder="0" style="position: relative; top: -36px"></iframe></div>');
    }
    else {
    echo '<img src="http://i0.tvguide.com/<?php $key="scene_number"; echo get_post_meta($post->ID, $key, true); ?>/576x324.jpg" width="576" height="324" alt="" />';
}
?>

我的代码以纯文本形式返回:

<div style="width: 645px; height: 364px; overflow: hidden"> <iframe src="http://tvguide.com<?php $key="scene_number"; echo get_post_meta($post->ID, $key, true); ?>/" width="645" height="430" scrolling="no" frameborder="0" style="position: relative; top: -36px"></iframe></div> 

有人可以告诉我我做错了什么吗? 谢谢!

1 个答案:

答案 0 :(得分:1)

你写错了php语法请尝试下面的代码。

<?php
$scene_trailer = get_post_meta($post->ID, 'scene_trailer', true);
if ($scene_trailer) {
    $key = "scene_number";
    $scene_number = get_post_meta($post->ID, $key, true);
    echo htmlentities('<div style="width: 645px; height: 364px; overflow: hidden">
        <iframe src="http://tvguide.com/' . $scene_number . '" width="645" height="430" scrolling="no" frameborder="0" style="position: relative; top: -36px"></iframe></div>');
} else {
    echo '<img src="http://i0.tvguide.com/scenes/' . $scene_number . '/576x324.jpg" width="576" height="324" alt="" />';
}
?>