0

结合分类法和用户角色,我列出了税收“狗”的所有术语,并且对于每个术语“狗”,列出了与用户配置文件相关联的所有“颜色”。例子:

  • 用户 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 }?>
4

2 回答 2

1

默认情况下,PHP 会尝试将对象转换为字符串进行比较。这导致了错误。您可以指定第二个参数array_unique()来覆盖它:

foreach(array_unique($users, SORT_REGULAR) as $user)
于 2013-11-17T15:46:02.247 回答
0

问题的解决方案,非常感谢@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 } ?>
于 2013-11-18T13:26:18.203 回答