Display post tags Wordpress

Discussion in 'Programming' started by chaukar, Jan 10, 2011.

  1. #1
    Simple question:

    <?php the_tags(); ?>

    How can I limit it to display only 3 tags or the first three tags.

    I've tried some other code from Wordpress doc..but couldn't get it work!
     
    chaukar, Jan 10, 2011 IP
  2. iTippy

    iTippy Peon

    Messages:
    10
    Likes Received:
    0
    Best Answers:
    0
    Trophy Points:
    0
    #2
    Could you possibly explain a little further in detail so I can attempt to help you out? From what I've read, you can create separate "post_tags();" and list them (each of them including one tag, or an array for wordpress).
     
    iTippy, Jan 10, 2011 IP
  3. Cash Nebula

    Cash Nebula Peon

    Messages:
    1,197
    Likes Received:
    67
    Best Answers:
    0
    Trophy Points:
    0
    #3
    That function is only good for changing the separators between the tags. To limit the number of tags, you need to use get_the_tags() and a loop counter.

    The code you want has already been posted on the Wordpress forums.
    http://wordpress.org/support/topic/show-only-the-first-three-tags
    
    <?php
    $count = 0;
    $posttags = get_the_tags();
    if ($posttags) {
    foreach($posttags as $tag) {
    $count++;
    if ($count <= 3 ) {
    echo '<a href="' . get_tag_link($tag->term_id) . '">' . $tag->name . '</a> ';
    }
    }
    }
    ?>
    
    Code (markup):
     
    Cash Nebula, Jan 11, 2011 IP