Currently I am displaying the following on my site which works well.
<?php if ( get_post_meta($post->ID, 'phone', true) ) : ?>
<strong>phone:</strong> <?php echo $phone; ?><?php endif; ?></div>
However I am now getting 2 variables 'phone' & 'mobile'.
So I want to only display one at a time with phone as the primary variable.
So what i need is the snippet of code to display.
<?php if ( get_post_meta($post->ID, 'phone', true) ) : ?>
<strong>phone:</strong> <?php echo $phone; ?><?php endif; ?>
<?php if ( get_post_meta($post->ID, 'phone', false) ) : ?>
<strong>mobile:</strong> <?php echo $mobile; ?><?php endif; ?>
But it displays both.
So I want to echo the 'phone' as primary, and if no 'phone', it echos the 'mobile'.
Denzel Chia answers:
Hi,
Please use this
<?php
$phone = get_post_meta($post->ID, 'phone', true);
$mobile = get_post_meta($post->ID, 'mobile', true);
if(!empty($phone)){
echo "phone: $phone";
}else{
echo "mobile: $mobile";
}
?>
Thanks.
Denzel
Sébastien | French WordpressDesigner answers:
<?php
$phone = get_post_meta($post->ID, 'phone', true);
$mobile = get_post_meta($post->ID, 'mobile', true);
if($phone) {echo "phone : $phone";}
elseif($mobile){echo "mobile: $mobile";}
?>
this code is complete :
if there is a phone, this number is displayed
else the "mobile" is displayed (only if "mobile" exist)
when you want use a condition like : if...else, the syntax must be like that
if (condition) { the action i want to do is here; }
elseif (another_condition) { the action i want to do if the second condition is true, and the first is false; }
Nilesh shiragave answers:
try PHP ifelse conditions
<?php if ( get_post_meta($post->ID, 'phone', true) ) {
$phone=get_post_meta($post->ID, 'phone', true);
?>
phone: <?php echo $phone; ?><?php } ?>
<?php elseif ( get_post_meta($post->ID, 'mobile', false) ) {
$mobile=get_post_meta($post->ID, 'mobile', true);
?>
mobile: <?php echo $mobile; ?><?php } else {} ?>
Tamilmozhi Gunasekar answers:
Try this
<?php if ( $phone = get_post_meta($post->ID, 'phone', true) ) : ?>
phone: <?php echo $phone; ?>
<?php else : ?>
<?php if ( $mobile = get_post_meta($post->ID, 'mobile', true) ) : ?>
mobile: <?php echo $mobile; ?>
<?php endif; ?>
<?php endif; ?>
Jimish Gamit answers:
<?php if ( $phone = get_post_meta($post->ID, 'phone', true) ) : ?>
phone: <?php echo $phone; ?>
<?php else : ?>
<?php $mobile = get_post_meta($post->ID, 'mobile', true) ?>
mobile: <?php echo $mobile; ?>
<?php endif; ?>