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

时间:2015-07-31 07:22:42

标签: php html wordpress advanced-custom-fields

我是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) } }

3 个答案:

答案 0 :(得分:0)

要检查变量中是否有某些内容使用empty()语言构造(manual)和is_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>';
    }
}

答案 1 :(得分:0)

通过

检查您的if (count($fields) > 0) { ... } 是否包含任何数据
{{1}}

答案 2 :(得分: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>';
 }  
?>