【问题标题】:Advanced Custom Fields (ACF) - loop through repeater checkout field and output single instance of each value高级自定义字段 (ACF) - 循环通过中继器结帐字段并输出每个值的单个实例
【发布时间】:2018-03-02 07:03:13
【问题描述】:

我有一个循环浏览产品列表的 WordPress 页面。产品是使用 ACF 转发器字段创建的。每个产品都有一个或多个属性(例如浅色、深色、金属色、彩色),使用复选框字段应用。

目的是查询页面上所有产品的复选框重复字段,查看单独分配给每个产品的属性,并为所有产品输出一个列表(去除重复项)。

例如,如果有 10 个产品,并且它们之间被标记了四个唯一属性,则只会输出四个唯一属性。

我尝试创建一个空数组,然后循环遍历它。当前循环输出重复值(例如light dark light dark light dark而不是light dark

<?php if (have_rows('product')): while (have_rows('product')) : the_row(); 
    $attributes = get_sub_field_object('product_attributes'); 
    $values = $attributes['value'];  
    if($values) {
        $filter_attributes = array();
        foreach ($values as $value) {
            if (in_array($attributes, $filter_attributes)) {
                continue;
            }
        $filter_attributes[] = $attributes;
        echo $value . " ";
    } 
} endwhile; endif; ?>

【问题讨论】:

  • $values 来自哪里?我想应该是$attributes?
  • 已更新到 $attributes,循环仍然产生重复
  • 我假设您没有得到任何结果,因为您的问题非常含糊(“到目前为止没有骰子”并没有给出任何问题的迹象)。所以问题实际上是$filter_attributes 数组有重复?那么它是如何工作的,因为您仍在 foreach 循环中使用 $values$values 是什么,它如何具有正确的值?我不确定您的代码要做什么。您希望if (in_array...) continue 实现什么目标?
  • 感谢您的帮助,已更新以显示 $values 是 ACF 复选框值。我正在尝试输出每个属性的单个实例
  • 你的逻辑全错了。例如,您希望通过if (in_array...) continue 实现什么目标?这没有任何影响。

标签: php arrays wordpress foreach


【解决方案1】:

您的代码存在很多问题,因此我们确实需要重新开始。根据您提供的内容,并且不知道您的 ACF 中继器是如何设置的,我认为以下内容应该适合您。

看起来你想做的是:

  1. 遍历中继器中的所有产品
  2. 从您的product_attributes 获取所有值
  3. 获取所有产品的唯一
  4. 在同一行显示唯一值,用空格分隔

您遇到的主要问题是获取唯一数组。有很多方法可以做到这一点,你选择了最复杂的:)

1.使用 in_array 检查以前的值

这是您目前尝试这样做的方式,但是您遇到了逻辑问题。因此,我建议选项 2,但为了完整起见,您应该这样做:

$filter_attributes = array();

if (have_rows('product')): while (have_rows('product')) : the_row(); 
    $attributes = get_sub_field_object('product_attributes'); 
    $values = $attributes['value'];  
    if($values) {
        foreach ($values as $value) 
            if (!in_array($value, $filter_attributes)) {
                $filter_attributes[] = $value;
            }
    } 
} endwhile; endif;

/* display the values on the same line, separated by spaces */
echo implode(" ", $filter_attributes );

2。使用 array_unique

此代码比前一个选项简单得多。您将 所有 值保存到一个数组中,然后最后使用 array_unique (PHP.net array_unique)。这消除了每次检查现有值的需要。例如

$all_attributes = array();
$filter_attributes = array();

if (have_rows('product')): while (have_rows('product')) : the_row(); 
    $attributes = get_sub_field_object('product_attributes'); 
    $values = $attributes['value'];  
    if($values) {
        foreach ($values as $value) 
            $all_attributes[] = $value; /* Save ALL values */
    } 
} endwhile; endif;

/* use array_unique to remove duplicates from the array */
$filter_attributes = array_unique ($all_attributes);
/* display the values on the same line, separated by spaces */
echo implode(" ", $filter_attributes );

3.使用数组键

如果您使用数组键的值,那么这将确保每个值都是唯一的,因为数组中不允许重复键。这有点 hack-y 的方式,但它又快又简单 :)

$filter_attributes = array();

if (have_rows('product')): while (have_rows('product')) : the_row(); 
    $attributes = get_sub_field_object('product_attributes'); 
    $values = $attributes['value'];  
    if($values) {
        foreach ($values as $value) 
            /* if  $all_attributes[$value] already exists, this will overwrite it 
               ensuring the array only has unique values */
            $all_attributes[$value] = $value;
    } 
} endwhile; endif;

/* display the values on the same line, separated by spaces */
echo implode(" ", $filter_attributes );

【讨论】:

  • @SBWT 很高兴我能帮上忙!如果答案有帮助,您可以接受它以使其已解决,以便其他有类似问题的用户知道它有帮助。我们都会得到一些代表:)
猜你喜欢
  • 2018-08-15
  • 2015-06-03
  • 1970-01-01
  • 2021-03-01
  • 2014-07-09
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多