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(); ?>
unset($phrases[$rand_phrase_value]);
which index doesn't exist. Change it tounset($phrases[$rand_phrase]);
. This will help.re-initializing
the array for each iteration of loop with same elements. Check this out: codepad.org/UoAr3Sef