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.
Any help of how to achieve this?
Many thanks.
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
The Call – insert this in your template page
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,
rand
is not an available option or$arg
forget_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
wordpress random tag issue
https://wordpress.stackexchange.com/questions/26112/how-to-limit-and-display-tag
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
Please reply with any mistakes noticed so I can edit them.