• Trying to create a php script that produces a random WP category name. So if there are 116 categories currently in the host dbase, the script will identify and use this number (cat_ID ???) to calculate a (rand() value. The resulting random number will correspond with a category name.

    So far my solution has been to create static php arrays of all known WP categories directly into the php script. This makes knowing all categories and calculating a random value quite easy. However, this will obviously always require maintenance to the script as new categories are added to the dbase, as well as make a script that will constantly bloat with more and more arrays. I’m wanting this script to be dynamic, flexible, and lightweight (as it should be). It could be a simple WP plugin when done. Anyone? Skippy???

    Example of my current script:

    //WP categories
    $categories = array("A9" ,"Google" ,"MSN" ,"Yahoo");

    //enter number of categories from above into rand
    $random = (rand()%4);

    echo ("$categories[$random]");

Viewing 2 replies - 1 through 2 (of 2 total)
  • Changes below should add the dynamic element you’re looking for:

    //WP categories
    $categories = $wpdb->get_col("SELECT cat_name FROM $wpdb->categories");

    //enter number of categories from above into rand
    $random = (rand()%count($categories));

    echo $categories[$random];

    Thread Starter kiddeath91

    (@kiddeath91)

    Kafkaesqui, beautiful! Thanks.

Viewing 2 replies - 1 through 2 (of 2 total)
  • The topic ‘Category randomizer.’ is closed to new replies.