I'm trying to add col-lg-6 to one of the Divs when no content is being displayed for name3 and name4, but it keeps showing as col-lg-4.
<?php
$name3 = get_field('name3', $post->ID);
$name4 = get_field('name4', $post->ID);
?>
<div class="col-lg-3">
<!-- name1 exists -->
</div>
<?php if ($name3 == "" || $name4 == ""): ?>
<div class="col-lg-4">
<?php elseif ($name3 == "" && $name4 == ""): ?>
<div class="col-lg-6">
<?php else: ?>
<div class="col-lg-3">
<?php endif; ?>
<!-- name2 exists -->
</div>
<div class="col-lg-3">
<!-- name3 does not exist -->
</div>
<div class="col-lg-3">
<!-- name4 does not exist -->
</div>
I've looked on here and other sites but I just can't figure it out. I know it's probably going to be something stupid and simple but I really need to sort this out now.
Simply reverse order of checks:
// if both are empty
<?php if($name3 == "" && $name4 == ""): ?>
<div class="col-lg-6">
// if one of them is empty
<?php elseif($name3 == "" || $name4 == ""): ?>
<div class="col-lg-4">
<?php else: ?>
<div class="col-lg-3">
<?php endif; ?>
otherwise first if () is true when one of variables is empty and if both are empty
also you can just change || (or) operator to xor operator in your first if ()
Currently when your second if
could be valid, it goes anyway to the first one. To resolve it, swap their order.
Change :
<?php if($name3 == "" || $name4 == ""): ?>
<div class="col-lg-4">
<?php elseif($name3 == "" && $name4 == ""): ?>
<div class="col-lg-6">
<?php else: ?>
<div class="col-lg-3">
<?php endif; ?>
To :
<?php if($name3 == "" && $name4 == ""): ?>
<div class="col-lg-4">
<?php elseif($name3 == "" || $name4 == ""): ?>
<div class="col-lg-6">
<?php else: ?>
<div class="col-lg-3">
<?php endif; ?>