plugins - Display a text message if the field is not found and not if found

admin2025-06-01  2

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 *

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 *

Share Improve this question edited Apr 3, 2019 at 21:49 Nicolas Logerot asked Apr 3, 2019 at 16:50 Nicolas LogerotNicolas Logerot 676 bronze badges
Add a comment  | 

3 Answers 3

Reset to default 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';
    }
?>

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';

    } 
?>

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';
    }
?>
转载请注明原文地址:http://conceptsofalgorithm.com/Algorithm/1748790620a313754.html

最新回复(0)