Php WordPress-在自定义字段中使用条件

Php WordPress-在自定义字段中使用条件,php,wordpress,conditional-statements,custom-fields,Php,Wordpress,Conditional Statements,Custom Fields,我下面的代码没有输出值。我想这与我连接回音字符串的方式有关。如果我去掉HTML标记,它将输出,但我需要添加样式 有没有更好的方法来实现这一点 <?php $location = get_post_meta( get_the_ID(), 'location', true ); $date = get_post_meta( get_the_ID(), 'date', true ); $season = get_post_meta( get_the_ID(), 'seas

我下面的代码没有输出值。我想这与我连接回音字符串的方式有关。如果我去掉HTML标记,它将输出,但我需要添加样式

有没有更好的方法来实现这一点

<?php
    $location = get_post_meta( get_the_ID(), 'location', true );
    $date = get_post_meta( get_the_ID(), 'date', true );
    $season = get_post_meta( get_the_ID(), 'season', true );
    if( !empty( $location ) && !empty($date) && !empty($season) ) {
        echo '<div class="post-extras">';
        echo '<p>' . 'Wedding Location: ' . $location . '</p>';
        echo '<p>' . 'Shoot Date: ' . $date . '</p>';
        echo '<p>' . 'Season: ' . $season . '</p>';
        echo '</div>';
    }
?>

是的,有:

echo <<<EOT

  <div class="post-extras">
    <p>Wedding Location: {$location}</p>
    <p>Shoot Date: {$date}</p>
    <p>Season: {$season}</p>
  </div>

EOT;

echo你也可以这样做

<?php if( !empty( $location ) && !empty($date) && !empty($season) ):?>
<div class="post-extras">
     <p>Wedding Location: <?php echo $location;?></p>
     <p>Shoot Date: <?php echo $date;?></p>
     <p>Season: <?php echo $season; ?></p>
</div>
<?php else:?>
  <h4>Sorry, nothing to show.</h4>
<?php endif;?>
如果您只想显示实际拥有的值::您可以使用:

<?php 
$location = get_post_meta( get_the_ID(), 'location', true ); 
$date = get_post_meta( get_the_ID(), 'date', true ); 
$season = get_post_meta( get_the_ID(), 'season', true ); ?>
 <div class="post-extras">
<?php
if( $location): ?>
     <p>Wedding Location: <?php echo $location?></p> 
<?php endif;?>
<?php if($date):?>
     <p>Shoot Date: <?php echo $date?></p> 
<?php endif;?>
<?php if($season):?>
     <p>Season: <?php echo $season?></p> 
<?php endif;?>
</div>

婚礼地点:

拍摄日期:

季节:


什么是更好的方式?所以你没有那么多的
echo
?您尝试使用例如
if($allthingshere):
并像
endif一样关闭它然后你就可以使用普通的html了,这意味着一种“更干净”的方式,使用更少的代码。感觉太重了。这件看起来更干净,但对我来说还是不行。我不明白。@NatashaMcd你从这些变量中得到什么值了吗?也许其中一个是空的,所以条件永远不满足。。。使用
else:
输出一些东西我现在就可以输出了。我必须对所有三个都有值才能执行它。我需要条件语句,如果至少有一个值,则输出html。表达这一点是不是错了?我想我需要一个OR。是的,如果你不需要满足所有这些条件,最好在标题中添加“| |
”,但现在它会执行所有html,即使它们没有值。所以,如果“日期”只有一个值,它仍然会显示位置:和季节:好的,谢谢。我把它输出了,但它没有输出值。它将变量输出为文本,而不是其值。我将整个div包装在以下内容中:''不需要将
div
用引号括起来。您是否检查过变量是否与简单的
echo$date一起工作;回声季节etc?没有任何理由不能在PHP代码中输出变量值。记住最后一个
EOT语句必须出现在它自己的行上,并且出现在行的最开头(前面没有空格)。
$location = get_post_meta( get_the_ID(), 'location', true );
printf("values is %s", $location); 
$date = get_post_meta( get_the_ID(), 'date', true ); 
printf("values is %s", $date);
$season = get_post_meta( get_the_ID(), 'season', true );
printf("values is %s", $season);
<?php 
$location = get_post_meta( get_the_ID(), 'location', true ); 
$date = get_post_meta( get_the_ID(), 'date', true ); 
$season = get_post_meta( get_the_ID(), 'season', true ); ?>
 <div class="post-extras">
<?php
if( $location): ?>
     <p>Wedding Location: <?php echo $location?></p> 
<?php endif;?>
<?php if($date):?>
     <p>Shoot Date: <?php echo $date?></p> 
<?php endif;?>
<?php if($season):?>
     <p>Season: <?php echo $season?></p> 
<?php endif;?>
</div>