在数组wordpress自定义字段术语中获取重复的术语名称?

Posted

技术标签:

【中文标题】在数组wordpress自定义字段术语中获取重复的术语名称?【英文标题】:Getting duplicates term names in array wordpress custom fields terms? 【发布时间】:2020-07-07 08:54:37 【问题描述】:

这是我的代码,我得到了重复的值。

$results = array();
    $results['targetOptions'] = array();
    $selectOptions = array();
    $selectOptions = get_posts(
                        array(
                            'post_type' => 'collection',
                            'post_status' => 'publish',
                            'posts_per_page' => -1,
                            'post_parent' => 0,                 
                        )
                    );

                    $results['targetOptions'][] = array("text" => __('Select a Collection', 'theme'), "value" => '');

                    foreach ($selectOptions as $selectOption) :
                        if($sourceVal == '999999') 
                            $terms = get_the_terms( $selectOption->ID, 'window_style' ); 
                        else 
                            $terms = get_the_terms( $selectOption->ID, 'door_style' );
                        
                        foreach($terms as $term) 

                          $results['targetOptions'][] = array("text" => $term->name, "value" => 'other-product--' . $selectOption->ID . '--other_product_style--' . $sourceVal);
                        

                    endforeach;

                    $results['targetClass'] = '.dynamic-collection';

【问题讨论】:

【参考方案1】:
$selectOptions = get_posts(
                    array(
                        'post_type' => 'collection',
                        'post_status' => 'publish',
                        'posts_per_page' => -1,
                        'post_parent' => 0,                 
                    )
                );

                $results['targetOptions'][] = array("text" => __('Select a Collection', 'theme'), "value" => '');

                $term_value_windows = array();
                $term_value_doors = array();
                foreach ($selectOptions as $selectOption) :
                    if($sourceVal == '999999') 
                        $terms = get_the_terms( $selectOption->ID, 'window_style' ); 
                        foreach($terms as $term) 

                          if(in_array($term->name, $term_value_windows))
                            continue;
                           else
                            $term_value_windows[]=$term->name;                          
                            $results['targetOptions'][] = array("text" => $term->name, "value" => 'other-product--' . $term->term_id . '--other_product_style--' . $sourceVal);
                          

                        
                    else 
                        $terms = get_the_terms( $selectOption->ID, 'door_style' );
                        foreach($terms as $term) 

                          if(in_array($term->name, $term_value_doors))
                            continue;
                           else
                            $term_value_doors[]=$term->name;

                            $results['targetOptions'][] = array("text" => $term->name, "value" => 'other-product--' . $term->term_id . '--other_product_style--' . $sourceVal);
                          

                        
                           


                endforeach;


                $results['targetClass'] = '.dynamic-collection';

这是该问题的答案。

【讨论】:

以上是关于在数组wordpress自定义字段术语中获取重复的术语名称?的主要内容,如果未能解决你的问题,请参考以下文章

氧气 - 高级自定义字段重复器子字段 - 链接字段

Wordpress分类术语还有内容的仪表板页面?

如何在 Wordpress 编辑器中添加自定义字段并获取其值?

PHP 在Wordpress中获取自定义字段值

WordPress:在自定义帖子类型中获取模块名称的帖子 slug

PHP 在wordpress主题中获取自定义字段值