0

I have a number of pages with a tag of 'word'.

For each of these pages I want a btn/Div on the home page linking to that page.

This button will display a random word from an array.

I want the word on each button to be different so I have picked a random word in the array and then deleted that word.

My problem is the word isn't deleted from the array - if I echo the array count on each loop it stays the same.

How can I pick a random word form the array and then delete that word.

    <?php

        $frontAgrs = array(
            'post_type' => 'page',
            'tag' => 'word',
            'order' => 'ASC'
        );

        $frontLoop = new WP_Query($frontAgrs);

        if($frontLoop->have_posts()):
            while($frontLoop->have_posts()):
                $frontLoop->the_post();

        /*----Phrase-------------*/ 
        $phrases = ['Hello Sailor','Acid Test','Bear Garden','Botch A Job','Dark Horse',
        'In The Red','Man Up','Pan Out','Quid Pro Quo','Rub It In','Turncoat',
        'Yes Man','All Wet','Bag Lady','Bean Feast','Big Wig'];

        $rand_Num = array_rand($phrases);
        $rand_phrase = $phrases[$rand_Num];
        unset($phrases[$rand_phrase]);      
        echo count($phrases);       

    ?>

    <?php echo '<div><a href="'.get_permalink($post->ID).'"><p>'.$rand_phrase_value.'</p></a></div>' ?>


    <?php endwhile; endif; ?>

    <?php wp_reset_postdata(); ?>
  • You are using unset($phrases[$rand_phrase_value]); which index doesn't exist. Change it to unset($phrases[$rand_phrase]);. This will help. – Rohit Pande Jun 14 '13 at 8:51
  • Thanks Rohit Pande but that still doesn't work. It still shows the array as the same count. I've updated the naming of the variables. – Simon Cooper Jun 14 '13 at 9:11
  • Check this out codepad.org/LjBpQdoo – Rohit Pande Jun 14 '13 at 9:19
  • Hi Rohit - I might have this wrong but if I but your code in a loop like it would be in wordpress the elements aren't removed and the count stays the same - codepad.org/duv3AeXE – Simon Cooper Jun 14 '13 at 13:24
  • This is happening because you are re-initializing the array for each iteration of loop with same elements. Check this out: codepad.org/UoAr3Sef – Rohit Pande Jun 15 '13 at 5:44
1

This is happening because you are re-initializing the phrases array on each iteration. Define that array outside the loop and this code will work.

<?php

        $frontAgrs = array(
            'post_type' => 'page',
            'tag' => 'word',
            'order' => 'ASC'
        );

        $frontLoop = new WP_Query($frontAgrs);

/*----Phrase-------------*/ 
            $phrases = array('Hello Sailor','Acid Test','Bear Garden','Botch A Job','Dark Horse',
            'In The Red','Man Up','Pan Out','Quid Pro Quo','Rub It In','Turncoat',
            'Yes Man','All Wet','Bag Lady','Bean Feast','Big Wig');

        if($frontLoop->have_posts()):
            while($frontLoop->have_posts()):
                $frontLoop->the_post();

        $rand_Num = array_rand($phrases);
        $rand_phrase = $phrases[$rand_Num];
        unset($phrases[$rand_Num]);      
        echo count($phrases);       

    ?>

    <?php echo '<div><a href="'.get_permalink($post->ID).'"><p>'.$rand_phrase_value.'</p></a></div>' ?>


    <?php endwhile; endif; ?>

    <?php wp_reset_postdata(); ?>

Hope this solution works for you.

Edit:

See the working here.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.