如果有值,如何只显示自定义 php 字段

How to only show custom php field if there is a value

我是 php 的新手,目前正在使用 wordpress 的高级自定义字段插件。我目前有这段 php 代码:

<?php

$group_ID = 110;

$fields = array();
$fields = apply_filters('acf/field_group/get_fields', $fields, $group_ID);

if ($fields) {
    echo '<div class="promotion-area">';
    foreach ($fields as $field) {
        $value = get_field($field['name']);
        echo '<div>' . $value . '</div>';

    }
    echo '</div>';
}
?>

我正在尝试仅在有值可查看时才显示 <div class="promotion-area">,如果没有可用值则删除 div。

如有任何帮助,我们将不胜感激。

这是一个var_dump:

array(2) { [0]=> array(18) { ["key"]=> string(19) "field_55bae192c069d" ["label"]=> string(15) "Promotion Title" ["name"]=> string(15) "promotion_title" ["_name"]=> string(15) "promotion_title" ["type"]=> string(4) "text" ["order_no"]=> int(0) ["instructions"]=> string(0) "" ["required"]=> int(0) ["id"]=> string(25) "acf-field-promotion_title" ["class"]=> string(4) "text" ["conditional_logic"]=> array(3) { ["status"]=> int(0) ["rules"]=> array(1) { [0]=> array(3) { ["field"]=> string(4) "null" ["operator"]=> string(2) "==" ["value"]=> string(0) "" } } ["allorany"]=> string(3) "all" } ["default_value"]=> string(0) "" ["placeholder"]=> string(0) "" ["prepend"]=> string(0) "" ["append"]=> string(0) "" ["formatting"]=> string(4) "html" ["maxlength"]=> string(0) "" ["field_group"]=> int(110) } [1]=> array(17) { ["key"]=> string(19) "field_55bae1d7c069f" ["label"]=> string(17) "Promotion Content" ["name"]=> string(17) "promotion_content" ["_name"]=> string(17) "promotion_content" ["type"]=> string(8) "textarea" ["order_no"]=> int(1) ["instructions"]=> string(0) "" ["required"]=> int(0) ["id"]=> string(27) "acf-field-promotion_content" ["class"]=> string(8) "textarea" ["conditional_logic"]=> array(3) { ["status"]=> int(0) ["rules"]=> array(1) { [0]=> array(3) { ["field"]=> string(4) "null" ["operator"]=> string(2) "==" ["value"]=> string(0) "" } } ["allorany"]=> string(3) "all" } ["default_value"]=> string(0) "" ["placeholder"]=> string(0) "" ["maxlength"]=> string(0) "" ["rows"]=> int(4) ["formatting"]=> string(2) "br" ["field_group"]=> int(110) } }

要检查变量中是否有内容,请使用 empty() 语言构造 (manual) with is_array() method to make sure that your variable is an array (manual):

if (is_array($fields) && !empty($fields)) {
    // do something if var $fields is not empty
}

看你的结果数组不是空的,总是通过以上条件,你需要foreach两次检查是否有你的值,然后显示它。

if (!empty($fields)) {
    $values = array();
    foreach ($fields as $field) {
        $value = get_field($field['name']);
        if (!empty($value)) {
            $values[] = $value;
        }
    }
    if (!empty($values)) {
        echo '<div class="promotion-area">';
        foreach ($values as $value) {
            echo '<div>' . $value . '</div>';
        }
        echo '</div>';
    }
}

通过

检查您的 $fields 是否保存了任何数据
if (count($fields) > 0) {
   ... 
} 

我已经更改了您的代码。试试这个

<?php
  $group_ID = 110;

   $fields = array();
   $fields = apply_filters('acf/field_group/get_fields', $fields, $group_ID);

  if (!empty($fields)) {          
   foreach ($fields as $field) {
    $value[] = get_field($field['name']);        
   }         
 }
 if(!empty($value)) {
  echo '<div class="promotion-area">'; 
  foreach ($value as $result) {
    $new_value = $result;
    echo '<div>' . $new_value . '</div>';
  }            
  echo '</div>';
 }  
?>