[英]Advanced Custom Fields (ACF) - loop through repeater checkout field and output single instance of each value
I have a WordPress page that loops through a list of products. 我有一个WordPress页面,可循环浏览产品列表。 The products are created using ACF repeater field.
使用ACF转发器字段创建产品。 Each product has a one or more attributes (eg light, dark, metallic, colourful) applied using the checkbox field.
每个产品都有一个或多个使用复选框字段应用的属性(例如,浅色,深色,金属色,彩色)。
The aim is to query the checkbox repeater fields for all products on the page, see what attributes are assigned to each product on an individual basis, and output a single list for all products (stripping out duplicates). 目的是查询页面上所有产品的复选框转发器字段,查看分别为每个产品分配了哪些属性,并输出所有产品的单个列表(去除重复项)。
For example, if there were ten products and between them they were tagged with four unique attributes, only the four unique attributes would be output. 例如,如果有十种产品,并且在它们之间用四个唯一属性标记,则仅输出四个唯一属性。
I've tried creating an empty array and then looping through that. 我试过创建一个空数组,然后遍历整个数组。 The current loop outputs duplicate values (eg
light dark light dark light dark
instead of light dark
) 电流回路输出重复的值(例如,
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; ?>
There are quite a few issues with your code so we really need to start again. 您的代码有很多问题,因此我们确实需要重新开始。 Based on what you've provided and without knowing exactly how your ACF repeater is set up, I think the following should work for you.
根据您所提供的内容,并且不完全了解ACF中继器的设置方式,我认为以下内容应该对您有用。
What it appears that you want to do is: 您似乎想要做的是:
product_attributes
product_attributes
获取所有值 The main problem you are having is getting the unique array. 您遇到的主要问题是获取唯一数组。 There are a number of ways to do this, you chose the most complicated :)
有很多方法可以做到这一点,您选择了最复杂的:)
1. Use in_array to check previous values 1.使用in_array检查以前的值
This is the way you are trying to do it at the moment, but you are having problems with the logic. 目前,您正在尝试通过这种方式来执行此操作,但是逻辑上存在问题。 Therefore I'd suggest option 2, but for completeness this is how you should do it:
因此,我建议使用选项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. Use array_unique 2.使用array_unique
The code for this is much simpler than the previous option. 此代码比以前的选项简单得多。 You save all values into an array and then at the end use
array_unique
( PHP.net array_unique ). 您将所有值保存到数组中,然后最后使用
array_unique
( PHP.net array_unique )。 This eliminates the need for checking the existing values every time. 这样就无需每次检查现有值。 eg
例如
$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. Use the array key 3.使用阵列键
If you use the value for the array key, then that will ensure each values will be unique because duplicate keys are not allowed in the array. 如果将值用于数组键,则将确保每个值都是唯一的,因为数组中不允许重复的键。 It's slightly hack-y way, but its quick & easy :)
这有点骇人听闻,但快速又容易:)
$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 );
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.