dt102282 2014-02-13 16:05
浏览 50
已采纳

WordPress - 使用自定义字段的条件

My code below is not outputting the values. I think it has something to do with how I've concatenated the echo strings. If I strip away the HTML tags it will output, but I need to add styling.

Is there a better way to achieve this?

<?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>';
    }
?>
  • 写回答

2条回答 默认 最新

  • douba6361 2014-02-13 16:30
    关注

    well you can do it like this as well.

    <?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;?>
    

    Update

    <?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( $location && $date && $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 endif;?>
    

    and to debug it, you add something like this so you are sure you are getting the right values, or any values out of the get_post_meta

    $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);
    

    and in case you only want to show the values you actually have:: you can use this::

    <?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>
    
    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论
查看更多回答(1条)

报告相同问题?

悬赏问题

  • ¥20 ML307A在使用AT命令连接EMQX平台的MQTT时被拒绝
  • ¥20 腾讯企业邮箱邮件可以恢复么
  • ¥15 有人知道怎么将自己的迁移策略布到edgecloudsim上使用吗?
  • ¥15 错误 LNK2001 无法解析的外部符号
  • ¥50 安装pyaudiokits失败
  • ¥15 计组这些题应该咋做呀
  • ¥60 更换迈创SOL6M4AE卡的时候,驱动要重新装才能使用,怎么解决?
  • ¥15 让node服务器有自动加载文件的功能
  • ¥15 jmeter脚本回放有的是对的有的是错的
  • ¥15 r语言蛋白组学相关问题