Forums

How to limit tags returned by the_tags (7 posts)

  1. Drew Baker
    Member
    Posted 2 years ago #

    Hi Guys,
    Is there a way to limit the amount of tags returned by the_tags template tag?

    I'd like to just display the first 4 instead of all of the tags for a given post.

    Thanks!

  2. esmi
    Theme Diva & Forum Moderator
    Posted 2 years ago #

    You could try using get_the_tags instead and then just display the first 4 tags in the returned array.

  3. Drew Baker
    Member
    Posted 2 years ago #

    Hey esmi,
    I tried something like this but only got one tag returned. Any help? My PHP sucks.

    <?php
    $posttags = get_the_tags();
    $count=0;
    if ($posttags) {
    foreach($posttags as $tag) {
    $count++;
    if (4 == $count) {
    echo $tag->name . ' ';
    }
    }
    }
    ?>

    Thanks!

  4. esmi
    Theme Diva & Forum Moderator
    Posted 2 years ago #

    <?php
    $posttags = get_the_tags();
    $count=0;
    if ($posttags) {
    	$output = '';
    	foreach($posttags as $tag) {
    		$count++;
    		$output .= $tag->name . ' ';
    		if( $count >4 ) break;
    	}
    }
    echo $output;
    ?>
  5. VincentvandenHeuvel
    Member
    Posted 2 years ago #

    @esmi can you also limit the get_tag_link to 4?

  6. Drew Baker
    Member
    Posted 2 years ago #

    Hey VincentvandenHeuvel did you ever figure out how to get this to return just 4 tag links? The example esmi gave just returns words, not links. Thanks.

  7. alchymyth
    The Sweeper
    Posted 2 years ago #

    @dbaker
    read this:
    http://codex.wordpress.org/Function_Reference/get_the_tags
    http://codex.wordpress.org/Function_Reference/get_tag_link

    <?php
    $posttags = get_the_tags();
    $count=0;
    if ($posttags) {
    	foreach($posttags as $tag) {
    		$count++;
    		echo '<a href="'.get_tag_link($tag->term_id).'">'.$tag->name.'</a> ';
    		if( $count >4 ) break;
    	}
    }
    ?>

Topic Closed

This topic has been closed to new replies.

About this Topic