1

I am trying to display a message like "Sorry no information here" if a field is not found and if field is found, no message should appear. I am using the ACF plugin for displaying fields on the front end in a user profile.

This is my code:

<?php $user_ID = get_current_user_id(); 

            $label = 'user_' . $user_ID;
            the_field('Information', $label); ?>

If anyone has an idea, I have tried a lot of things but nothing helps. Thanks in advance. Nico

*Update: In fact, with ACF plugin for wordpress, I have created a field to display in user account in the backend of wordpress. So now I want to display this field (information) in the frontend account of the user.
If I need to put a message for my client, I put this message in the wordpress backend user account.
So in the frontend account, if I don't have put any message in this field (information), I want to display an automatic message like: "You don't have information yet"
If I put a message in the field (information) in the wordpress backend user account, I want the automatic message disappear and my message is displayed.

for now the message I put in field (information) in the backend account user is displayed correctly in the frontend user account *

3

Are you just looking for a simple if then statement? Assuming that your fields are set up properly, then this code should work:

<?php 
    $user_ID = get_current_user_id(); 
    $label = 'user_' . $user_ID;

    if (get_field('Information', $label)) {
      //there is data;
    } else { 
      echo 'Sorry no information here';
    }
?>
  • Nothing appears, same if information field is empty or not, no echo and no information are displayed – Nicolas Logerot Apr 3 at 21:34
  • try adding an echo to replace the line //there is data. You should get something one way or another. That statement is either true or false. – rudtek Apr 3 at 22:01
  • Yes it works ! I have already try but I put a syntax error so thank you for all my friend! – Nicolas Logerot Apr 3 at 22:53
2

the_field() echos the field value, while get_field() returns the field value which can be checked using if statement to display a message. Documentation

<?php 

$user_ID = get_current_user_id(); 

$label = 'user_' . $user_ID;

//the_field('Information', $label);

if (! get_field('Information', $label)) {

      echo 'Sorry no information here';

    } 
?>
  • 1
    echo always display, if information field is empty or have content – Nicolas Logerot Apr 3 at 21:33
  • Yes, the_field('Information', $label); is equivalent to echo get_field('Information', $label); – Qaisar Feroz Apr 4 at 11:26
0

I finally find the solution, thanks you Rudtek, and thank you Qaisar Feroz for all of your time and code.

So this is the working code:

<?php 
    $user_ID = get_current_user_id(); 
    $label = 'user_' . $user_ID;

    if (get_field('information', $label)) {
      //there is data;
      echo get_field('galerie', $label);
    } else { 
      echo 'Sorry no information here';
    }
?>

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

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