Take the 2-minute tour ×
WordPress Development Stack Exchange is a question and answer site for WordPress developers and administrators. It's 100% free, no registration required.

Hi i need to show random 3 tags for a post. all my posts have been tagged with more than 10 tags, i need to display any 3 tags randomly.

for eg:

A sample post

<a href="/tag1">Tag1</a>
<a href="/tag2">Tag2</a>
<a href="/tag3">Tag3</a>

I am currently using <?PHP the_tags()?> to generate all tags how to restrict it to 3 randomly generated tags.

share|improve this question

1 Answer 1

up vote 1 down vote accepted

Try this:

   $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;
       }
   }
share|improve this answer
    
spot on ,i need to make it random. bcoz every time it shows first three tag(in ascending ord ). how to make it random? –  Ezhil Aug 17 '11 at 5:53
    
just add shuffle($posttags); before $count=0; –  Sean Lee Aug 17 '11 at 7:54

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.