• Im within taxonomies (a) archive and I want to loop through all the posts and retrieve a list of terms for taxonomy (b) then echo that list out with no duplicates. Forgive me as I am new to wordpress and php but here’s what I have so far in my taxonomy.php.

    <?php while (have_posts()) : the_post();
    $terms = get_the_term_list( $post->ID, ‘role-type’, ‘ Role(s): ‘, ‘ ‘, ‘ ‘ );
    echo $terms;
    endwhile; ?>

    Lets say there are three posts in the archive this then displays the following

    Role(s): DOS FE RSM RSS Role(s): DOS FE SPMs RSS Role(s): CSO

    What it should do is display something similar to: Role(s): DOS FE RSM RSS SPMS CSO

    What do I need to do? Thanks for your help

Viewing 2 replies - 1 through 2 (of 2 total)
  • I can’t entirely test this, but it should be close:

    <?php $all_terms = '';
    $sep = '|';
    $taxonomy = 'role-type';
    while (have_posts()) : the_post();
        $terms = get_the_term_list( $post-ID, $taxonomy, '', $sep, $sep );
        $all_terms .=  $terms;
    endwhile;
    $terms_array = explode($sep, $all_terms);
    $terms_array = array_unique($terms_array);
    $all_terms = implode(' ', $terms_array);
    echo "<p>Roles: $all_terms</p>";
    ?>
    Thread Starter dustmop

    (@dustmop)

    That worked modified it a little bit.

    <?php $array_out = array();
    while (have_posts()) : the_post();
        $terms = get_the_terms( $post->ID, 'role-type');
        foreach($terms as $term){
            $term_link = get_term_link($term, 'role-type');
            $array_out[] = '<a href="'.$term_link.'">'.$term->name.'</a>';
        }
    endwhile;
    
    $array_clean = array_unique($array_out);
    echo ' ' . implode(', ', $array_clean) . '</p>'; } ?>

    Now how would one do that same thing but create a list of all the posts hierarchical terms list of another different taxonomy? So loop through all posts and spit out

    Parent (a)
    -Children
    Parent (b)
    -Children

    For a taxonomy but with no duplicates.

    Heres what I have so far

    <?php
     $array_out = array();
     while (have_posts()) : the_post();
    $taxonomy = 'workflow-type'; // change this to your taxonomy
    $terms = wp_get_post_terms( $post->ID, $taxonomy, array( "fields" => "ids" ) );
    if( $terms ) {
      echo '<ul>';
    
      $terms = trim( implode( ',', (array) $terms ), ' ,' );
      wp_list_categories( 'title_li=&taxonomy=' . $taxonomy . '&include=' . $terms );
    
      echo '</ul>';
    }
    endwhile;
    ?>

    But it doesnt filter out duplicates and diplays something like this:

    Parent (a)
    – Child
    Parent (a)
    – Child
    Parent (b)
    – Child
    Parent (a)
    – Child

Viewing 2 replies - 1 through 2 (of 2 total)
  • The topic ‘loop through posts and get the list of all terms for a taxonomy’ is closed to new replies.