Created
January 25, 2013 19:31
-
-
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.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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