Skip to content

Instantly share code, notes, and snippets.

@zumwalt
Created January 25, 2013 19:31
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save zumwalt/4637132 to your computer and use it in GitHub Desktop.
Save zumwalt/4637132 to your computer and use it in GitHub Desktop.
A WordPress function for randomly showing a set number of items from a taxonomy. To use, include <?php random_tax(); ?> in your markup.
function random_tax() {
$counter = 0;
$max = 5; // Set max number of items here.
$cats ='';
$categories=get_categories();
$rand_keys = array_rand($categories, $max);
foreach ($rand_keys as $key) {
$cats .= $categories[$key]->term_id .',';
}
$args=array(
'include' => $cats
);
$categories=get_categories($args);
foreach($categories as $category) {
$counter++;
if ($counter <= $max) {
echo '<a href="">'. $category->name.'</a>, ';
} else {
echo 'or <a href="">'. $category->name.'</a> ';
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment