2017-09-21 1 views
0

제품 목록을 반복하는 WordPress 페이지가 있습니다. 제품은 ACF repeater 필드를 사용하여 작성됩니다. 각 제품에는 확인란 필드를 사용하여 적용된 하나 이상의 속성 (예 : 밝음, 어두움, 금속성, 다채로운)이 있습니다.ACF (Advanced Custom Fields) - 반복기 체크 아웃 필드를 반복하고 각 값의 단일 인스턴스를 출력합니다.

목표는 페이지의 모든 제품에 대한 확인란 중계기 필드를 쿼리하고 각 제품에 개별적으로 할당 된 속성을 확인하고 모든 제품에 대해 단일 목록을 출력 (중복 제거)하는 것입니다.

예를 들어, 10 개의 제품이 있고 그 사이에 4 개의 고유 한 속성이 태그 된 경우 4 개의 고유 한 특성 만 출력됩니다.

빈 배열을 만든 다음이를 반복 해 보았습니다. 현재 루프 출력 값을 중복 (예 : 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; ?> 
+0

? 나는 그것이'$ 속성들 '이어야한다고 생각하니? – FluffyKitten

+0

$ 속성으로 업데이트했습니다. 루프가 계속 중복을 생성합니다. – SBWT

+0

질문이 매우 모호했기 때문에 아무런 결과도 얻지 못했다고 생각했습니다 ("지금까지는 주사위를 잘못 쓰지 않아도 무엇이 잘못되었는지 알 수 없습니다"). 그래서 문제는 사실'$ filter_attributes' 배열에 중복 된 것이 있습니까? 'foreach' 루프에서 여전히'$ values ​​'를 사용하고 있기 때문에 어떻게 동작합니까? '$ values ​​'는 무엇이고 올바른 값을 가지는 방법은 무엇입니까? 귀하의 코드가 무엇을 하려는지 확실하지 않습니다. 'if (in_array ...) '에 의해 달성하고자하는 것은 무엇입니까? – FluffyKitten

답변

0

코드와 매우 몇 가지 문제가 그래서 우리는 정말 다시 시작할 필요가 있습니다. 당신이 제공 한 것에 기초하여 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. 중복 키가 배열에서 허용되지 않기 때문에 각 값이 고유해질 수있게됩니다 그 다음, 당신은 배열 키 값을 사용하는 경우

키 배열입니다.그것은 약간의 방법-Y를 해킹,하지만 것의 빠른 & 쉽게 :`$의 values`에서 오는

입니다
$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); 
+0

!! 감사합니다 – SBWT

+0

@SBWT 기꺼이 도와 드리겠습니다! 답변이 도움이되면 해결 된 것으로 받아 들여 유사한 문제가있는 다른 사용자에게 도움이 될 것입니다. 그리고 우리 둘 다 일부 담당자를 얻을거야 :) – FluffyKitten

관련 문제