This php function, written for wordpress, gives you the opportunity to grab a X amount of category id’s, excluding empty one’s.
It accepts just one parameter which would be the amount of category id’s you want.
After the function has done it’s job, it will return an array, containing the amount of requested category id’s.
<?php
/**
* PickRandomCategory()
* Returns X category ids.
* @param int $amount
* @return array $return
*/
function PickRandomCategory($amount){
if (!is_int($amount))
return false;
mt_srand((double)microtime()*1000000);
$ids = array();
$return = array();
$cats = get_categories(array('hide_empty' => 1));
foreach ($cats as $cat)
$ids[] = $cat->cat_ID;
shuffle($ids);
for ($j = 0; $j < $amount; $j++){
$return[] = $ids[$j];
}
return $return;
}
// $random_cat will now have 3 array elements - filled with id's.
$random_cat = PickRandomCategory(3);
?>
Mail this!
- Comments (0)
- PingBacks (0)
- TrackBacks (0)


» Latest comments