结合分类法和用户角色,我列出了税收“狗”的所有术语,并且对于每个术语“狗”,列出了与用户配置文件相关联的所有“颜色”。例子:
- 用户 1 具有与配置文件一起存储的元“狗:金毛猎犬”和“颜色:黄色”。
- 用户 2 具有与配置文件一起存储的元“狗:金毛猎犬”和“颜色:黑色”。
- 用户 3 具有与配置文件一起存储的元“狗:金毛猎犬”和“颜色:黑色”。
下面的标题 3 个标签吐出“术语名称”= 在本例中为“狗”。在狗下面,应该有一个通过用户元链接到这条狗的“颜色”列表 - IE、黄色、黑色。重要的是“黑色”只出现一次,而不是两次。我正在尝试删除重复项,但出现错误。
编辑:错误不再存在。但是,现在 - echo $array 只回显“黑色”,而不是“黄色”或任何其他颜色。
有什么想法吗?
<?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 }?>