Support » Fixing WordPress » get random sub categories with get_categoreis

  • Hi,
    I have about 100 sub directories under category 3 and I would like display random 12 sub directories every time people refresh the page.

    this is what I have so far but it’s not returning anything
    My first help would be: how to retrieve all the sub categories id and store them into an array? and once that stored into an array I can then do a random function to route that array.

    $args=array(
    
      'child_of' => '3',
      'number' =>'12'
      );
    $categories=get_categories($args);
    
    foreach ($categories as $cat) : ?>

    thanks

Viewing 1 replies (of 1 total)
  • I think this is what you want:

    $args=array(
      'child_of' => '3',
    );
    $categories = get_categories($args);
    $number_to_show = min(12, sizeof($categories));
    $picks = array_rand($categories,$number_to_show);
    shuffle($picks);
    foreach ($picks as $pick) {
       $cat = $categories[$pick];
       echo "<p>Category = $cat->name</p>";
    }
Viewing 1 replies (of 1 total)
  • The topic ‘get random sub categories with get_categoreis’ is closed to new replies.