0

I'd like to show all the taxonomies related to the posts in a category page (archive).

I've the following code

<?php 
    $cat1 = $cat;
    $args = array( 'posts_per_page' => -1, 'category' => $cat1 );?>
    $myposts = get_posts( $args );
    foreach ( $myposts as $post ) {
        $product_terms = wp_get_object_terms($post->ID, 'Brands');
        echo '<ul>';
        foreach(array_unique($product_terms) as $term) {
            echo '<li><a href="'.get_term_link($term->slug, 'Brands').'">'.$term->name.'</a></li>'; 
        }
        echo '</ul>';
    }
?>

but it returns duplicates for taxonomies. I tried array_unique but it doesn't work. Could you please help. Thanks

frnhr
  • 12,354
  • 9
  • 63
  • 90
advalue
  • 15
  • 4

1 Answers1

0

Separate the loops, like this:

<?php 
    $cat1 = $cat;
    $args = array( 'posts_per_page' => -1, 'category' => $cat1 );?>
    $myposts = get_posts( $args );
    $myterms = array();
    foreach ( $myposts as $mypost ) {
        foreach ( wp_get_object_terms($mypost->ID, 'Brands') as $term ) {
            $myterms[ $term->term_id ] = $term;
        }
    }
    echo '<ul>';
    foreach( $myterms as $term ) {
        echo '<li><a href="'.get_term_link($term->slug, 'Brands').'">'.$term->name.'</a></li>'; 
    }
    echo '</ul>';
?>

If there are many terms, the approach above can result in PHP memory error. Here is another approach:

<?php 
    $cat1 = $cat;
    $args = array( 'posts_per_page' => -1, 'category' => $cat1 );?>
    $myposts = get_posts( $args );
    $visited_term_ids = array();
    echo '<ul>';
    foreach ( $myposts as $mypost ) {
        foreach ( wp_get_object_terms($mypost->ID, 'Brands') as $term ) {
            if ( ! isset( $visited_term_ids[ $term->term_id ] ) ) {
                echo '<li><a href="'.get_term_link($term->slug, 'Brands').'">'.$term->name.'</a></li>'; 
                $visited_term_ids[ $term->term_id ] = TRUE;
            }
        }
    }
    echo '</ul>';
?>
frnhr
  • 12,354
  • 9
  • 63
  • 90
  • thanks frnhr for you reply. Unfortunately your suggestion doesn't work. Where Am I wrong? Thanks for your support – advalue Oct 31 '13 at 15:55
  • Exactly how it breaks (I'm not saying that I'm curtain it works, I've written the code here without testing it)? Put `var_dump($myterms)` after the first loop and look at the output, that should provide some clue in to what is going wrong. – frnhr Oct 31 '13 at 16:22
  • There is no a specific error, simply the page doesn't show up or better I got the header then nothieng.... but after your second suggestion, I have: array(295) { [0]=> object(stdClass)#11933 (9) { ["term_id"]=> string(3) "471" ["name"]=> string(4) "Asus" ["slug"]=> string(4) "asus" ["term_group"]=> string(1) "0" ["term_taxonomy_id"]=> string(3) "481" ["taxonomy"]=> string(6) "Marche" ["description"]=> string(0) "" ["parent"]=> – advalue Oct 31 '13 at 17:11
  • if I remove array_unique() it shows me the list of the taxonomies (Brands or Marche (in italian)) but it is still duplicated. Thanks for your help – advalue Oct 31 '13 at 17:57
  • probably these page could be useful: http://wordpress.org/support/topic/multiple-queries-compiling-into-one-loop and http://wordpress.stackexchange.com/questions/100216/eliminate-duplicates-in-a-foreach-loop – advalue Oct 31 '13 at 18:31
  • great! your second approach works. Thanks a lot!! Is it possibile order the taxonomy by name or smth else? – advalue Oct 31 '13 at 19:40
  • To sort them you'd have to take the first approach, and then do something like this: http://stackoverflow.com/a/4282423/236195 between the loops. – frnhr Oct 31 '13 at 21:17