Support » Plugins and Hacks » Show only the first three tags

  • How would I go about this? I’ve tried using get_the_tags(), but while I can set up the PHP correctly to cycle three times, it seems I can’t actually access the data…

    $tags = get_the_tags(); if ($tags) { for ($i=0;$i<=2;$i+=1) { echo $tags[$i]->name . ' '; } }

Viewing 8 replies - 1 through 8 (of 8 total)
  • From the Codex example at get_the_tags

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

    Or look at get_the_tag_list.

    I’m pretty sure that both of those suggestions will display all of the tags.

    Sorry missed the goal 😉

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

    jdcfsu

    (@jdcfsu)

    Is it possible to do this but also make the tags links to the tag archive?

    Varsity

    (@varsity)

    Yes: use get_tag_link().

    jdcfsu

    (@jdcfsu)

    Where would you use that? Inserting it in place of get_the_tags() breaks the code.

    Varsity

    (@varsity)

    This should do the job:

    <?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> ';
    }
    }
    }
    ?>
    jdcfsu

    (@jdcfsu)

    Cool. Thank you.

Viewing 8 replies - 1 through 8 (of 8 total)
  • The topic ‘Show only the first three tags’ is closed to new replies.