Take the 2-minute tour ×
WordPress Development Stack Exchange is a question and answer site for WordPress developers and administrators. It's 100% free, no registration required.

I'm building a Directory in Wordpress, and using Advanced Custom Fields for the client to populate the directory entries. One of the sections is for Social Media icons.

Using the following code, I can check to see if any of the three fields (twitter / facebook / linked-in) is empty, and if empty to not show an icon:

<li><?php if(get_field('twitter') != ""): ?><a href="<?php the_field('twitter'); ?>" class="twitter">Twitter<span></span></a></li>
<?php else: ?><?php endif; ?> 
<li><?php if(get_field('facebook') != ""): ?><a href="<?php the_field('facebook'); ?>" class="facebook">Facebook<span></span></a></li>
<?php else: ?><?php endif; ?>
<li><?php if(get_field('linked_in') != ""): ?><a href="<?php the_field('linked_in'); ?>" class="linked">Linked In<span></span></a></li>
<?php else: ?><?php endif; ?>

I need help in working how to check to see if all 3 fields are empty to show additional text. (E.g There are no social icons to display).

I currently have:

<?php if(get_field('linked_in') && get_field('twitter') && get_field('facebook') != ""): ?>
<span class="small">There are no Social Media links to display</span><?php endif; ?>

However, using this code shows nothing when all 3 fields are empty - but shows the text 'There are no social...' when all 3 fields have values instead!

I'd be very appreciative if anyone can send me in the right direction on this one. I'm pretty much still a newbie, but trying hard to learn so please be gentle!

share|improve this question
add comment

2 Answers

Note that get_field returns false and not an empty string when no value is found. It's not written in the docs there but you can try a var_dump on an empty get_field item to confirm.

So you should check them against false. I've broken down each condition on its own line for the sake of simplicity:

<?php 
if(
    ( false === get_field('linked_in') ) && #no linked_in value
    ( false === get_field('twitter') ) && #no twitter value
    ( false === get_field('facebook') )  #no facebook value
  ): 
?>
<span class="small">There are no Social Media links to display</span>
<?php endif; ?>

Test for return value of get_field on an item that does not exist:

var_dump( get_field('dummy_string',232) );
// results: bool(false) 
share|improve this answer
    
$var = false; var_dump( empty($var) ); // true –  Max Yudin Feb 12 '13 at 12:35
    
$var = ''; var_dump( empty($var) ); // true –  Max Yudin Feb 12 '13 at 12:36
    
Yep, but why call another function when we can compare against false directly? –  RRikesh Feb 12 '13 at 12:41
    
empty() will also return true for integer 0, float 0.0 and string 0, which could potentially be a saved post meta value. –  RRikesh Feb 12 '13 at 12:50
    
Hi rrikesh. Thanks so much for the help; it's greatly appreciated. However, when I said I'm 'pretty much a newbie' I wasn't exaggerating! I'm not sure what I need to do with the var_dump - and where exactly to put it. If you can help further that'd be fantastic - but no worries if not. –  user2064265 Feb 12 '13 at 12:51
show 2 more comments

Your if-statement wont work the way you want it.

Change this part of the code

if(get_field('linked_in') && get_field('twitter') && get_field('facebook') != "")

Into

if(empty(get_field('linked_in')) && empty(get_field('twitter')) && empty(get_field('facebook')))

This way all three fields will be checked whether they are empty or not.

share|improve this answer
    
Hi Mike. Many thanks for your help, it's much appreciated - but unfortunately I'm getting this error when using that code: Fatal error: Can't use function return value in write context –  user2064265 Feb 12 '13 at 12:46
add comment

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.