How to display a list of random tags in wordpress?

I have used the following code to try and display a list of random tags:

<?php wp_tag_cloud('smallest=10&largest=10&number=5&format=list&order=rand&orderby=count'); ?>

This does display a list of 5 tags, but the order isn’t random, and doesn’t change when I reload the page.

Read More

Any help of how to achieve this?

Many thanks.

Related posts

Leave a Reply

1 comment

  1. I was looking for an answer to this myself tonight and I thought I would post a solution.

    My requirements were for random tags displayed each page refresh and the ability to exclude certain tags. For my purposes I put it in functions.php because I hate cluttering up my template files with functions.

    The Function – insert this in your functions.php file

    function jr_get_tags_but_exclude() {
        $args = array('exclude' => '36 17'); // see notes below
            $alltags = get_tags( $args );
            shuffle($alltags);
            $count=0;
            if ($alltags) {
                foreach($alltags as $tag) {
                    $count++;
                    echo '<a href="'.get_tag_link($tag->term_id).'">'.$tag->name.'</a>';
            if( $count >19 ) break;
            }
        }
    }
    

    The Call – insert this in your template page

    <?php jr_get_tags_but_exclude(); ?>
    

    Notes

    • To exclude tags you will need the tag ID . The simplest way to get the ID is to go to posts -> tags and hover over the tag you want to exclude. The ID # will be given in the url bar on the bottom of your browser when hovered on, &tag_ID=9, for example.

    • The WordPress codex for get_tags says to put only a blank space seperating each tag ID, no commas required.

    • Regarding randomizing the results, randis not an available option or $arg for get_tags, unfortunately.

    • To change the number of tags shown adjust if ( $count >19 ) 19 = 20 tags, 29 = 30, etc.

    • shuffle($alltags); is what does the randomization

    • $count=0; starts the counter, $count++; counts each tag posted, if( $count >19 ) break; ends the loop, in this case after 20 tags are posted.

    Credits and References

    mostly based on these gent’s solutions

    1. wordpress random tag issue

    2. https://wordpress.stackexchange.com/questions/26112/how-to-limit-and-display-tag

    3. http://wordpress.org/support/topic/php-exclude-tag

    wordpress codex for get_tags

    http://codex.wordpress.org/Function_Reference/get_tags

    In Your Template File – no function needed

    <?php
        $args = array('exclude' => '36 17'); // see notes below
            $alltags = get_tags( $args );
            shuffle($alltags);
            $count=0;
            if ($alltags) {
                foreach($alltags as $tag) {
                    $count++;
                    echo '<a href="'.get_tag_link($tag->term_id).'">'.$tag->name.'</a>';
            if( $count >19 ) break;
            }
        }
    ?>
    

    Please reply with any mistakes noticed so I can edit them.