• Resolved Dave Meier

    (@hiddendepth)


    Page I am trying to update can be viewed here
    I have setup taxomony as my services on projects. I display them in a list get by this

    <?php
    					$taxonomy = 'my_services';
    					$terms = get_the_terms( $post->ID , 'mytags' );
    
    					if ( !empty( $terms ) ) :
    
    					echo '<ul>';
    
    					foreach ( $terms as $term ) {
    						$link = get_term_link( $term, $taxonomy );
    						if ( !is_wp_error( $link ) )
    							echo '<li class="' . $term->slug. '"><a href="' . $link . '" rel="tag">' . $term->name . '</a></li>';
    					}
    
    					echo '</ul>';
    
    					endif;
    					?>

    I basically want to limit the number of tags display on the index of this page just like the excerpt does for content.
    THen I can show the full list inside the single page.

Viewing 5 replies - 1 through 5 (of 5 total)
  • put a counter into the foreach loop, and break the foreach if the threshold is reached:

    for instance:

    foreach ( $terms as $term ) {
    if($counter++ >= 20) break;
    		$link = get_term_link( $term, $taxonomy );
    		if ( !is_wp_error( $link ) )
    		echo '<li class="' . $term->slug. '"><a href="' . $link . '" rel="tag">' . $term->name . '</a></li>';
    			}
    Thread Starter Dave Meier

    (@hiddendepth)

    that is perfect.
    thanks

    is it possible that could then filter by either most popular tags or i could add in an if statement which listed say 10 different tags. So if these tags are present add them into the list before anything else but still filter it down by number of tags too.

    Thread Starter Dave Meier

    (@hiddendepth)

    I have an issue with how the code works in the loop. It seems to be stopping the tag count at a set number for the entire loop.

    So i want to filter only 5 tags for each post in the loop. What it is doing is stopping at 5 tags total so post 2+ does not have any tags.

    my bad – i assumed that this would only be used once on a page – correction
    (need to set the counter to zero before the foreach)

    $counter = 0:
    foreach ( $terms as $term ) {
    if($counter++ >= 20) break;
    		$link = get..........
    Thread Starter Dave Meier

    (@hiddendepth)

    you my friend are a genius

    thanks a lot. really making life easier with small tips like this.
    If its ok I will pick your brain on a few other things over the next few days.

    Thanks again

Viewing 5 replies - 1 through 5 (of 5 total)
  • The topic ‘Need to limit number of tags displayed’ is closed to new replies.