The below code looks at my wordpress site to determine if a field has a value and if so, displays it inside a div with a header.
<?php if (get_post_meta($post->ID, '_one_wysiwyg', true)){ ?>
<div class="section1">
<div style="float:<?php echo $direction; ?>"><h3>Jack's Two Cents</h3></div>
<?php echo get_post_meta($post->ID, '_one_wysiwyg', true); ?>
</div>
<?php }
if (get_post_meta($post->ID, '_two_wysiwyg', true)){ ?>
<div class="section2">
<div style="float:<?php echo $direction; ?>"><h3>Jill's Two Cents</h3></div>
<?php echo get_post_meta($post->ID, '_two_wysiwyg', true); ?>
</div>
<?php }
if (get_post_meta($post->ID, '_three_wysiwyg', true)){ ?>
<div class="section3">
<div style="float:<?php echo $direction; ?>"><h3>George's Two Cents</h3></div>
<?php echo get_post_meta($post->ID, '_three_wysiwyg', true); ?>
</div>
<?php }
if (get_post_meta($post->ID, '_four_wysiwyg', true)){ ?>
<div class="section4">
<div style="float:<?php echo $direction; ?>"><h3>Sally's Two Cents</h3></div>
<?php echo get_post_meta($post->ID, '_four_wysiwyg', true); ?>
</div>
<?php }
if (get_post_meta($post->ID, '_five_wysiwyg', true)){ ?>
<div class="section5">
<div style="float:<?php echo $direction; ?>"><h3>Alex's Two Cents</h3></div>
<?php echo get_post_meta($post->ID, '_five_wysiwyg', true); ?>
</div>
<?php } ?>
For those that don't know, the if statements look inside the database for the given post's field called #_wysiwyg and whether the field has a value. If it has no value, the section is ignored and nothing is displayed for that section.
You'll notice an undefined variable $direction inside the style tag of the div containing the h3 (header) of each grouping.
I want to be able to alternate the float for each grouping between left and right so that the header will be on the right side for the first group, on the left for the second, right for the third, left for the fourth and right for the fifth.
The reason I need to do this dynamically is obviously because all five groups won't always be displayed. Some posts may have two, some four, some none at all.
Is this possible in PHP? If so, how? and if not, how?
:nth-child(odd)for this, instead of using php