WordPress.org

Ready to get started?Download WordPress

Forums

get random sub categories with get_categoreis (2 posts)

  1. radiofranky
    Member
    Posted 1 year ago #

    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

  2. vtxyzzy
    Member
    Posted 1 year ago #

    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>";
    }

Topic Closed

This topic has been closed to new replies.

About this Topic

Tags

No tags yet.