简体   繁体   English

高级自定义字段(ACF)-遍历转发器签出字段并输出每个值的单个实例

[英]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: 您似乎想要做的是:

  1. Loop through all products in your repeater 遍历中继器中的所有产品
  2. Get all values from your product_attributes product_attributes获取所有值
  3. Get the unique values across all products 获得所有产品的独特价值
  4. Display the unique values on the same line, separated by spaces 在同一行上显示唯一值,以空格分隔

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_uniquePHP.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.

相关问题 高级自定义字段 - 循环遍历转发器子字段并显示每个转发器字段的特定子字段属性 - Advanced Custom Fields - looping through repeater subfields and displaying a specific subfield attribute of each repeater field 每个转发器字段的唯一ID(高级自定义字段) - Unique ID for each repeater field (advanced custom fields) 以编程方式更新转发器字段中特定组字段的值 - 高级自定义字段 (ACF) - Wordpress - Update the value of specific group field inside repeater field programmatically - Advanced Custom Field (ACF) - Wordpress 两列重复器字段高级自定义字段 - Two Column Repeater Field Advanced Custom Fields WordPress 高级自定义字段,循环中继器 - WordPress advanced custom fields, loop though repeater 根据另一个转发器值获取高级自定义字段转发器字段值 - Get advanced custom fields repeater field value based off of another repeater value WordPress-在通过博客帖子进行的搜索中包括高级自定义字段(ACF),并在搜索结果中显示这些字段 - WordPress - Including an Advanced Custom Field (ACF) in a search through blog posts and displaying the fields in the search results 如何在ACF转发器字段中的行上循环并回显值编号 - How to loop through and echo value number on rows within the ACF repeater field ACF转发器子字段随机播放(wordpress高级自定义字段) - ACF repeater sub-fields Shuffle (wordpress advance custom field) 在wordpress中循环浏览ACF中继器行,并分别显示每一行 - Loop through ACF repeater rows in wordpress and display each row separately
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM