• Bare with me this may sound complicated, but I know it’s not impossible.

    I want to limit the number of tags shown on my post on the homepage using this code <?php the_tags();?>. Now i read that to do this I must use the code below which I found on the wordpress Codex page.

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

    But there is a slight problem with that. One it doesn’t link and two it uses the $count=0; and I already use counts for my post loop.

    Here is the code I use for my loop

    <?php if (have_posts()) :
    $count = 0;
    while (have_posts()) : the_post();
        $count++;
        if ($count  == 1) : ?> <!--first box -->
            <div class="style-1"><?php the_content(); ?>
        <?php elseif ($count  > 1 && $count  <5) : ?></div> <!--next three boxes -->
            <div class="style-2"><?php the_content(); ?></div>
        <?php elseif ($count  == 5) : ?> <!-- box five and break div -->
            <div class="style-2"><?php the_content(); ?></div>
            <div class="first-break-div"><?php /* the content from first break div */ ?>    </div>
        <?php elseif ($count >5 && $count < 11) : ?> <!--next five boxes -->
            <div class="style-3"><?php the_content(); ?></div>
        <?php elseif ($count == 12) : ?> <!--last box and last break div -->
            <div class="style-3"><?php the_content(); ?></div>
            <div class="last-break-div"><?php /* the content from last break div */ ?>    </div>
        <?php endif;
     endwhile;
    endif; ?>

    The code above allows me to style multiple post differently. The first post is style one way. The next four post is style differently and so on.

    Now I can’t use the get_the_tags code above without it interfering with my loop. So how can I code the the_tags code to display only one tag. But only for the home page?

  • The topic ‘How do I limit number of tags shown using the_tags’ is closed to new replies.