Confused with this If Else statement for Custom Fields
-
This is basically to credit photo sources at the end of single.php. If no custom field is filled in, then it should return nothing. If there’s 1 field filled in, then return 1 field. If there’s 2, then return both fields and so on. I’m using this for custom fields.
I’m putting this at the beginning of my loop because that’s what’s required from the tutorial I’m using…
<?php if (have_posts()) : ?><?php while (have_posts()) : the_post(); $data = get_post_meta( $post->ID, 'key', true ); ?>This is the (admittedly) hacked together If Else statement..
<?php $cfield1 = get_post_meta( $post->ID, 'photo1', true ); ?> <?php $cfield2 = get_post_meta( $post->ID, 'photo2', true ); ?> <?php $cfield3 = get_post_meta( $post->ID, 'photo3', true ); ?> <?php $cfield4 = get_post_meta( $post->ID, 'photo4', true ); ?> <?php $cfield5 = get_post_meta( $post->ID, 'photo5', true ); ?> <?php if($cfield1) { ?> <a><href="<?php echo $cfield1; ?>">Photo Source (1)</a> <?php } if($cfield2) { ?> <a><href="<?php echo $cfield2; ?>">Photo Source (2)</a> <?php } if($cfield3) { ?> <a><href="<?php echo $cfield3; ?>">Photo Source (3)</a> <?php } if($cfield4) { ?> <a><href="<?php echo $cfield4; ?>">Photo Source (4)</a> <?php } if($cfield5) { ?> <a><href="<?php echo $cfield5; ?>">Photo Source (5)</a> <?php } else { ?> NOTHING <?php } ?>Any help would be greatly appreciated!
The topic ‘Confused with this If Else statement for Custom Fields’ is closed to new replies.