random custom field wordpress -
i have 3 custom fields on page, dailytip_1, dailytip_2 , dailytip_3.
so want show 1 tip of 3 tips randomly on page. can do? thanks!
<?php $dailytip_1 = get_post_meta($post->id, 'dailytip_1', true); ?> <?php if ($dailytip_1 !== '') { ?> <p><?php echo get_post_meta($post->id, 'dailytip_1', true); ?></p> <?php } ?>
does following help?
<?php $random_dailytip = 'dailytip_' . rand( 0, 2 ); $dailytip = get_post_meta($post->id, $random_dailytip, true); ?> <?php if ($dailytip !== '') { ?> <p><?php echo get_post_meta($post->id, $random_dailytip, true); ?></p> <?php } ?>
as can see, $random_dailytip
gets values dailytip_0
, dailytip_1
or dailytip_2
randomly.
Comments
Post a Comment