I have links of term on sidebar When i attach more than one post to term it get repeat.
$args = new WP_Query(array('post_type' => 'song', 'post_status' => 'publish', 'posts_per_page' => '-1'));
while ($args->have_posts()) : $args->the_post();
$terms = get_the_terms( $post->ID, 'song-categories' );
if ($terms && ! is_wp_error($terms)){
foreach($terms as $term) {
if ($term_id == $term->term_id){ $curent_term = ' class="current"'; } else {$curent_term = '';}
echo '<li><a href="'.get_term_link($term->slug, 'song-categories').'" class="'.$term->slug.'">'.$term->name.'</a></li>';
}
}
endwhile;
You aren't collecting all the terms here, you are collecting all the posts. 'posts_per_page' => '-1'
$args = new WP_Query(array('post_type' => 'song', 'post_status' => 'publish', 'posts_per_page' => '-1'));
while ($args->have_posts()) : $args->the_post();
So, for each post, you are getting all the terms:
$terms = get_the_terms( $post->ID, 'song-categories' );
You need to use a different method. In the past I have used things along the lines of:
function tax_list($tax, $current = null){
$terms = get_terms( $tax, 'orderby=count&hide_empty=1' );
foreach($terms as $term){
if($current == $term->term_id){
$class= " class='active'";
}else{
$class= "";
}
$name = $term->name;
$link = get_term_link( $term->slug, $tax );
echo "<li><a href='$link'$class>$name</a></li>";
}
}
Used as so:
tax_list($taxonomy, $term_id);