I build a code to eventually get a random item per category. Functions are working. I am just curious if the WHILE and IF are accurate. I am not sure if the first two unset() is good or do I need to drop those? And more important WHY the random ALWAYS give me the same result first result as if $myitems holds only one???


// loop from array //
foreach ($mycats as $catvalue) {

// get my items //

// check for items and has category name //
while ( has_items() && item_category()==$catvalue) {

    // store items in array by title //
    $myitems[] = item_title() ;

    // make the array random //
    $selected = array_rand(array_flip($myitems));

    // request items again but with random title //
    while( has_items() && ( item_title()==$selected)){ 

        // display my item //

    // unset varible arrays but hold catvalue //

// unset all variable arrays //

Is this wordpress by any chance?

Member Avatar

Seems to be a lot of unnecessary code in the loop. I think. ALso you have loads of user-defined functions that could be causing errors - we have no way of knowing.

$class seems to be holding the variable that decides on the output, but nowhere is this set in your snippet.