donglu7816 2014-03-16 19:24
浏览 29
已采纳

如果用户meta是空白回音的东西?

I'm trying to add a custom profile avatar to Wordpress. I have the field added to the profile and it saves everything fine, but I'm trying to make a conditional that if the user does not upload a custom file, it defaults to the gravatar.

I have this so far:

<?php if(get_the_author_meta('da_avatar') != ''): ?>
    <p>Exists!</p>
 <?php else: ?>
    <p>Does not exist!</p>
<?php endif; ?>

da_avatar is the ID of the custom field within the profile. This function always yields "DOES NOT EXIST" even if the field is not blank.

Any idea on why it is not working and how I can make it so that if the custom avatar field is empty, it will display "does not exist" and if it is not empty, have it display "Exists!"

  • 写回答

1条回答 默认 最新

  • douchao9899 2014-03-16 20:15
    关注

    I'm guessing it's because of where you're running this code. Try adding the User ID as the second parameter. I've also updated your code slightly to make it easier to use 'da_avatar' once you've performed the check.

    My suggestion would be to do the following:

    <?php if ( $da_avatar = get_the_author_meta( 'da_avatar', $user_id ) ) : ?>
        <p>Exists!</p>
    <?php else : ?>
        <p>Does not exist!</p>
    <?php endif; ?>
    

    Make sure you replace $user_id with a valid user ID. If that works let me know where you're running this code and I'll talk you through setting it up properly.

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?

悬赏问题

  • ¥15 微信会员卡接入微信支付商户号收款
  • ¥15 如何获取烟草零售终端数据
  • ¥15 数学建模招标中位数问题
  • ¥15 phython路径名过长报错 不知道什么问题
  • ¥15 深度学习中模型转换该怎么实现
  • ¥15 HLs设计手写数字识别程序编译通不过
  • ¥15 Stata外部命令安装问题求帮助!
  • ¥15 从键盘随机输入A-H中的一串字符串,用七段数码管方法进行绘制。提交代码及运行截图。
  • ¥15 TYPCE母转母,插入认方向
  • ¥15 如何用python向钉钉机器人发送可以放大的图片?