如果未找到该字段,则显示文本消息;如果未找到,则显示文本消息

时间:2019-04-03 作者:Nicolas Logerot

如果未找到字段,我试图显示类似“抱歉,此处无信息”的消息,如果找到字段,则不应显示任何消息。我正在使用ACF插件在用户配置文件的前端显示字段。

这是我的代码:

<?php $user_ID = get_current_user_id(); 

            $label = \'user_\' . $user_ID;
            the_field(\'Information\', $label); ?>
如果有人有想法的话,我已经尝试了很多东西,但没有任何帮助。提前谢谢。尼科

*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 个回复
最合适的回答,由SO网友:rudtek 整理而成

你只是在寻找一个简单的if-then语句吗?假设字段设置正确,则此代码应能正常工作:

<?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\';
    }
?>

SO网友:Qaisar Feroz

the_field() 回显字段值,而get_field() 返回可使用检查的字段值if 语句以显示消息。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\';

    } 
?>

SO网友:Nicolas Logerot

我终于找到了解决方案,谢谢你Rudtek,谢谢你Qaisar Feroz为我花费的时间和代码。

这就是工作代码:

<?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\';
    }
?>