【问题标题】:Wordpress - How to check whether an array is empty using PHP?Wordpress - 如何使用 PHP 检查数组是否为空?
【发布时间】:2018-10-23 15:52:00
【问题描述】:

我创建了一个用于图像信用的 PHP 函数,这使用户可以在需要时添加图像信用。

我注意到我创建的函数的一个问题,如果尚未填充图像信用字段,则图像信用类的背景颜色在 DOM 上可见。

<p class="image-credits"><?php if(!empty($creditsImg = get_post_meta(get_post_thumbnail_id(), '_wp_attachment_credits', true))); echo $creditsImg ?></p>

有没有办法阻止这种情况发生?

【问题讨论】:

    标签: php arrays wordpress


    【解决方案1】:

    PHP 有一个名为 empty() - https://secure.php.net/manual/en/function.empty.php 的内置函数

    这对很多事情都有好处,包括检查空数组。你可以这样使用:

    if (empty($array)) {
        # array is empty
    }
    

    if (!empty($array)) {
        # array is not empty
    }
    

    【讨论】:

      猜你喜欢
      • 2011-01-14
      • 2011-07-31
      • 2011-01-23
      • 1970-01-01
      • 1970-01-01
      • 2021-08-17
      • 2015-07-22
      • 2011-11-21
      相关资源
      最近更新 更多