array_unique - 렌더링 오류로 wp_user foreach의 중복을 제거하십시오?
-
21-12-2019 - |
문제
분류 및 사용자 역할을 결합하면서 모든 세금 "개"를 나열하고 사용자 프로필에 링크 된 모든 "색상"목록 인 "개"라는 용어가 있습니다.예 :
- 사용자 1은 메타 "Dog : Golden Retriever"및 "color : yellow"프로파일이 저장되어 있습니다.
- 사용자 2는 메타 "Dog : Golden Retriever"및 "Color : Black"프로파일이 저장됩니다.
- 사용자 3에는 메타 "Dog : Golden Retriever"및 "색상 : 검정"프로파일이 저장됩니다.
아래의 태그를 제목하면 "용어 이름"=이 경우 '개'가 될 것입니다.개 밑에, 사용자 메타를 통해이 개와 연결된 "색상"목록이 있어야합니다. 즉, 노란색, 검은 색. "흑백"은 두 번이 아니라 한 번만 나타나는 것입니다.중복을 제거하려고 노력하고 있지만 오류가 발생합니다.
편집 : 더 이상 오류가 지속되지 않습니다.그러나, 이제 - 에코 $ 어레이 만 "흑백"또는 다른 색상이 아닌 "검은 색".
어떤 생각?
<?php $terms = get_terms('dogs');
$count = count($terms);
if ( $count > 0 ){ foreach ( $terms as $term ) { ?>
<h3><?php echo $term->name; ?></h3> // Show different "dog" type names
<div class="listed_dogs_color_names">
// Now search all editor and contributor user profiles for "dog" and "color" user_meta. Color and dog are both taxonomies that are used both with posts and users (user meta)
// If matches with the "dog" above, list "color" underneath "dog name" in <h3></h3> above.
<?php
$term_parent = $term->parent;
$term_slug = $term->slug;
$editor_query = new WP_User_Query(
array(
'role' => 'editor',
'meta_key' => $term_parent,
'meta_compare' => '=',
'meta_value' => $term_slug,
)
);
$editors = $editor_query->get_results();
$contributor_query = new WP_User_Query(
array(
'role' => 'contributor',
'meta_key' => $term_parent,
'meta_compare' => '=',
'meta_value' => $term_slug,
)
);
$contribs = $contributor_query->get_results();
$users = array_merge( $contribs, $editors );
?>
<?php
$array = array(); // initialize as empty array ?>
<?php if (!empty($users)) {?>
<?php foreach ($users as $user) :
$b = $user->color;
$color = explode("\n", $b);
$array[] = $color[0];
?>
<?php endforeach; ?>
<?php $array = array_unique($array); ?>
<?php
echo "<li>";
echo $array[0];
echo "</li>";
?>
<?php } ?>
</div><!--close-->
<?php }?>
<?php }?>
. 해결책 2
문제에 대한 해결책, 나와 함께 걷는 @amal 덕분에 많은 감사
<?php if (!empty($users)) {?>
<ul>
<?php
$array = array();
foreach ($users as $user) :
$b = $user->color;
$color = explode("\n", $b);
$array[] = $color[0];
?>
<?php endforeach; ?> // close the foreach, looped through all users already and stored meta into array
<?php $array = array_unique($array, SORT_REGULAR); ?> // take array and remove dupes
<?php foreach ($array as $item) { ?> // foreach and style. can also implode and separate with commas via <?php $spitarray = implode ( ", ", $spitarray); ?>
<li><?php echo $item; ?></li>
<?php } ?>
</ul>
<?php } ?>
. 다른 팁
기본적으로 PHP는 객체를 문자열로 변환하려고 시도합니다.이로 인해 오류가 발생합니다. array_unique()
의 두 번째 매개 변수를 지정하여
foreach(array_unique($users, SORT_REGULAR) as $user)
. 제휴하지 않습니다 StackOverflow