I use Wordpress and ACF plugin to create add. fields on the site. In the admin there is a link to Facebook.

In the site template, it is displayed as follows:

<?php the_field('facebook', 'option'); ?> 

I want to make a condition for the withdrawal so that in the case of an empty field it is not displayed in the template. I tried this option:

 <?php if( (the_field('facebook', 'option'))!="") { ?> ---- <?php } ?> 

If not empty, output. If empty, then nothing should be displayed.

Tell me what am I doing wrong?

  • If you are given an exhaustive answer, mark it as correct (a daw opposite the selected answer). - Nicolas Chabanovsky

2 answers 2

Try this

 <?php if( !isset(the_field('facebook', 'option'))) { ?> 

if not, show what gives

 <?php var_dump(the_field('facebook', 'option')) ?> 
  • <? php var_dump (the_field ('facebook', 'option'))?> - returns NULL if the field is not filled. www.facebook.comNULL, if filled. Option 1 gave an error - Fatal error: Can't use the function return value in write context - drseo
  • then do <? php if ((the_field ('facebook', 'option'))! = "NULL") {?> or <? php if ((the_field ('facebook', 'option'))! = NULL) {?> - Sergalas
  • If the field was not filled, the condition <? Php if ((the_field ('facebook', 'option'))! = NULL) {?> Fulfilled and nothing was output. When the field was filled in, the following occurred: The content inside the condition was not displayed (Icon + link), but the completed link to facebook was simply displayed, it looks like the content was derived from this comparison. - drseo
  • I did not understand the last phrase. - Sergalas
  • <? php $ facebook = get_field ('facebook', 'option'); if ($ facebook! = "") {?> ----- <? php}?> - this option worked, using instead of the_field, get_field. Many thanks for the help and responsiveness! - drseo
 <?php $href= the_field('facebook', 'option'); // либо $href= get_field('facebook', 'option'); if( !empty($href) ) { echo $href; } ?> 

second option

 <?php $href = the_field('facebook', 'option'); // либо $href = get_field('facebook', 'option'); if( !empty($href) ): ?> <a href="<?php echo $href; ?>">ссылка на фейсбук</a> <?php endif; ?>