Domanda

Combinando tassonomie e ruoli utente, sto elencando tutti i termini di "cane" fiscale e per ogni termine "cane", un elenco di tutti i "colori" che sono collegati a un profilo utente.Esempio:

    .
  • Utente 1 ha meta "cane: Golden Retriever" e "Colore: Giallo" memorizzato con profilo.
  • Utente 2 ha Meta "Dog: Golden Retriever" e "Colore: nero" memorizzato con profilo.
  • utente 3 ha meta "cane: Golden Retriever" e "Colore: nero" memorizzato con profilo.

Heading 3 Tag qui sotto sputa il "nome del termine"= che in questo caso sarebbe "cane".Sotto il cane, dovrebbe esserci un elenco di "colori" che sono collegati a questo cane tramite utente meta - cioè giallo, nero. L'importante è che "Black" appare solo una volta, non due volte.Sto cercando di rimuovere i duplicati, ma ricevo un errore.

Modificato: l'errore non persiste più.Tuttavia, ora - l'echo $ array solo echos "nero", non "giallo" o qualsiasi altro colore.

Qualche idea?

<?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 }?>
.

È stato utile?

Soluzione 2

Soluzione al problema, molte grazie a @amal per averlo attraversato con me

<?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 } ?>
.

Altri suggerimenti

Per impostazione predefinita, PHP cercherà di convertire gli oggetti in stringhe per il confronto.Questo sta causando l'errore.È possibile specificare il secondo parametro di array_unique() per sovrascrivere questo:

foreach(array_unique($users, SORT_REGULAR) as $user)
.

Autorizzato sotto: CC-BY-SA insieme a attribuzione
Non affiliato a StackOverflow
scroll top