WordPress – Using Conditionals with Custom Fields

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

Related posts

Leave a Reply

2 comments

  1. 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>
    
  2. Yes there is:

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

    This is called a Heredoc, and is particularly useful in PHP for printing/echoing blocks of HTML code with visible indentation. The ability to throw a variable in there is pretty useful too.