Hide field when is empty

Discussion in 'WordPress' started by Divvy, Jul 8, 2019.

  1. #1
    Hello guys,

    Can someone help me with a little issue that I have in my code? :)

    I have the following custom fields in my functions.php:
    update_post_meta($post->ID, 'mr_month_price', $_POST['mr_month_price']);
    update_post_meta($post->ID, 'mr_month_off', $_POST['mr_month_off']);
    update_post_meta($post->ID, 'mr_month_lastrice', $_POST['mr_month_lastrice']);
    
    update_post_meta($post->ID, 'mr_sixmonth_price', $_POST['mr_sixmonth_price']);
    update_post_meta($post->ID, 'mr_sixmonth_off', $_POST['mr_sixmonth_off']);
    update_post_meta($post->ID, 'mr_sixmonth_lastrice', $_POST['mr_sixmonth_lastrice']);
    
    update_post_meta($post->ID, 'mr_threeyear_price', $_POST['mr_threeyear_price']);
    update_post_meta($post->ID, 'mr_threeyear_off', $_POST['mr_threeyear_off']);
    update_post_meta($post->ID, 'mr_threeyear_lastrice', $_POST['mr_threeyear_lastrice']);
    
    update_post_meta($post->ID, 'mr_fiveyear_price', $_POST['mr_fiveyear_price']);
    update_post_meta($post->ID, 'mr_fiveyear_off', $_POST['mr_fiveyear_off']);
    update_post_meta($post->ID, 'mr_fiveyear_lastrice', $_POST['mr_fiveyear_lastrice']);
    PHP:
    Today I added the following new custom fields:
    update_post_meta($post->ID, 'mr_trial_price', $_POST['mr_trial_price']);
    update_post_meta($post->ID, 'mr_trial_off', $_POST['mr_trial_off']);
    update_post_meta($post->ID, 'mr_trial_lastrice', $_POST['mr_trial_lastrice']);
    PHP:

    This is the code that I have in my single.php file:
    <?php } else { if($month_price_1){ ?>
    
    <ul class="wjprice">
    
    <li><a target="_blank" href="<?php echo $post_external_link; ?>" rel="nofollow">
    <div class="pnkP" id="p1"><span>$</span>
    <h5><?php echo get_post_meta($post->ID,"mr_month_price",true); ?> <small>/month</small></h5>
    </div>
    <div class="offP" id="p2">
    <?php $dollar_price_1= get_post_meta($post->ID,"mr_month_off",true); if($dollar_price_1){ ?>
    <i class="prctIcon"></i> <?php echo $dollar_price_1."% OFF"; } ?> </div>
    <div class="dscP" id="p3">
    <?php $dollar_my= get_post_meta($post->ID,"mr_month_lastrice",true);  if($dollar_my){ ?>
    <i class="curP"></i> <strike><?php echo $dollar_my; ?></strike>
    <?php } ?>
    </div>
    </a></li>
    
    <?php  } $mr_sixmonth_price_12=    get_post_meta($post->ID,"mr_sixmonth_price",true); if($mr_sixmonth_price_12){ ?>
    <li><a target="_blank" href="<?php echo $post_external_link; ?>" rel="nofollow">
    <div class="pnkP" id="p1"><span>$</span>
    <h5><?php echo get_post_meta($post->ID,"mr_sixmonth_price",true); ?> <small>/3 month</small></h5>
    </div>
    <div class="offP" id="p2">
    <?php $percentage_price_12= get_post_meta($post->ID,"mr_sixmonth_off",true);   if($percentage_price_12){ ?>
    <i class="prctIcon"></i> <?php echo $percentage_price_12."% OFF"; } ?></div>
    <div class="dscP" id="p3">
    <?php $my_year_123= get_post_meta($post->ID,"mr_sixmonth_lastrice",true); if($my_year_123){ ?>
    <i class="curP"></i> <strike><?php echo $my_year_123; ?></strike>
    <?php } ?>
    </div>
    </a></li>
    
    <?php } $mr_threeyear_price_123=get_post_meta($post->ID,"mr_threeyear_price",true); if($mr_threeyear_price_123){ ?>
    <li><a target="_blank" href="<?php echo $post_external_link; ?>" rel="nofollow">
    <div class="pnkP" id="p1"><span>$</span>
    <h5><?php echo get_post_meta($post->ID,"mr_threeyear_price",true); ?> <small>/6 month</small></h5>
    </div>
    <div class="offP" id="p2">
    <?php if(get_post_meta($post->ID,"mr_threeyear_off",true)){ ?>
    <i class="prctIcon"></i> <?php echo get_post_meta($post->ID,"mr_threeyear_off",true); ?>% OFF
    <?php } ?>
    </div>
    <div class="dscP" id="p3">
    <?php if(get_post_meta($post->ID,"mr_threeyear_lastrice",true)){ ?>
    <i class="curP"></i> <strike><?php echo get_post_meta($post->ID,"mr_threeyear_lastrice",true); ?></strike>
    <?php } ?>
    </div>
    </a></li>
    
    <?php } $mr_fiveyear_price_123=get_post_meta($post->ID,"mr_fiveyear_price",true); if($mr_fiveyear_price_123){ ?>
    <li><a target="_blank" href="<?php echo $post_external_link; ?>" rel="nofollow">
    <div class="pnkP" id="p1"><span>$</span>
    <h5><?php echo get_post_meta($post->ID,"mr_fiveyear_price",true); ?> <small>/1 Year</small></h5>
    </div>
    <div class="offP" id="p2">
    <?php if(get_post_meta($post->ID,"mr_fiveyear_off",true)){ ?>
    <i class="prctIcon"></i> <?php echo get_post_meta($post->ID,"mr_fiveyear_off",true); ?>% OFF
    <?php } ?>
    </div>
    <div class="dscP" id="p3">
    <?php $year_class_5_1= get_post_meta($post->ID,"mr_fiveyear_lastrice",true); if($year_class_5_1){ ?>
    <i class="curP"></i> <strike><?php echo $year_class_5_1; ?></strike>
    <?php } ?>
    </div>
    </a></li>
    
    <?php }?>
    </ul>
    <?php }?>
    PHP:
    At the moment, everything is working fine. The first one, "mr_month_price" is showing data when I put text inside the field and the other fields too, and if no text is added, they don't display, exactly as I want.

    I want to add the new custom fields "mr_trial_price" above "mr_month_price", in first.
    But when I do this, for some reason, the field "mr_month_price" disappears too when "mr_trial_price" field is empty...

    This is the code that I used after the line: <ul class="wjprice">:
    <?php } $mr_trial_price_123=get_post_meta($post->ID,"mr_trial_price",true); if($mr_trial_price_123){ ?>
    <li><a target="_blank" href="<?php echo $post_external_link; ?>" rel="nofollow">
    <div class="pnkP" id="p1"><span>$</span>
    <h5><?php echo get_post_meta($post->ID,"mr_trial_price",true); ?> <small>/trial</small></h5>
    </div>
    <div class="offP" id="p2">
    <?php if(get_post_meta($post->ID,"mr_trial_off",true)){ ?>
    <i class="prctIcon"></i> <?php echo get_post_meta($post->ID,"mr_trial_off",true); ?>% OFF
    <?php } ?>
    </div>
    <div class="dscP" id="p3">
    <?php if(get_post_meta($post->ID,"mr_trial_lastrice",true)){ ?>
    <i class="curP"></i> <strike><?php echo get_post_meta($post->ID,"mr_trial_lastrice",true); ?></strike>
    <?php } ?>
    </div>
    </a></li>
    PHP:
    Any idea of what could be?
    What I'm missing? :)

    Thanks!!
     
    Last edited: Jul 8, 2019
    Divvy, Jul 8, 2019 IP