Pick a page at random in Wordpress

Hi. I’m trying to write a wee function that will return a page link at random from the array generated by get_pages() in Wordpress. I created the following but it only works when $randomPageID equals 2, otherwise it echoes nothing. My website currently has 4 pages.


/*Create a function to pick a page at random from an array of the listed pages of the site */ 
function randomPage(){
	$pages = get_pages(); //grab list of pages array - Cf. http://codex.wordpress.org/Function_Reference/get_pages
	$randomPageID = mt_rand(1,count($pages)); //pick a page id at random from the list of page IDs
	foreach ($pages as $pagg) {
		if ($randomPageID == $pagg->ID) {
			$pageLink = get_page_link($pagg->ID);
			return '<li><a href="'.$pageLink.'">Pick a page at random.</a></li>';
		}
	}
}
echo randomPage();

Can you please help me figure out why this is? I also tried array_rand($pages), $n; but had trouble getting that to work as well.

Thanks,
Richard

Your page IDs probably aren’t the numbers 1 through 4. So generating a random number between 1 and 4 is not the same as picking a random ID from the 4 returned by get_pages().

But that’s alright because you don’t have to pick a random page ID. You don’t need a loop and a comparison. Just use the page in the $pages array corresponding to the random index you generated.

function randomPage(){ 
    $pages = get_pages(); //grab list of pages array - Cf. http://codex.wordpress.org/Function_Reference/get_pages 
    $random_key = mt_rand(1,count($pages)); //pick a page id at random from the list of page IDs 
    $page = $pages[$random_key];

    $pageLink = get_page_link($page->ID); 
    return '<li><a href="'.$pageLink.'">Pick a page at random.</a></li>'; 

} 
echo randomPage(); 

Beautiful, thanks Dan! You were spot on about the page IDs not equalling 1-4.

Incidentally, I had a play with the code you suggested and it worked great, apart from only outputting the last 3 of the pages and outputting a blank id instead of the first page’s id. However after subtracting 1 from mt_rand(1,count($pages)) before assigning the value $random_key it now works perfectly.

The final code, for anyone who’s interested :
(not that it’s very complicated, but I’m still a PHP newbie so I’m pretty stoked about it)


/* A function to pick a page at random from an array of the listed pages of the site */ 
function randomPage(){ 
	$pages = get_pages(); //grab list of pages array - Cf. http://codex.wordpress.org/Function_Reference/get_pages 
	$randomKey = (mt_rand(1,count($pages)) - 1); //pick a page id at random from the list of page IDs 
	$page = $pages[$randomKey];
	$pageLink = get_page_link($page->ID); //generate the page link for the selected page
	return '<li><a href="'.$pageLink.'">Pick a page at random</a></li>'; 
} 
echo randomPage(); 


Thanks again Dan! Much appreciated :slight_smile:

Oops, you’re right, I should’ve caught that. The random number needs to be between 0 and 1 less than the size of the array, because array indexes start at 0 not 1.

Hey Richard, how’s your cousin? :wink:

even easier would be to just use shuffle on the array then grab index 0

function randomPage(){ 
    $pages = get_pages(); //grab list of pages array - Cf. http://codex.wordpress.org/Function_Reference/get_pages 
    shuffle($pages);

    $pageLink = get_page_link($pages[0]->ID); 
    return '<li><a href="'.$pageLink.'">Pick a page at random.</a></li>'; 

} 
echo randomPage(); 

Thanks Mal :stuck_out_tongue:

And I dunno dude, ask her yourself: twitter.com/HayleyWestenra

Not to be sticking my nose in, are you sure you want a random Page, not a mpost? Most sites I know don’t have many pages…

Yeah that’s a good point, but nah I did mean pages not posts. The site will have a little over 20 pages when I’m finished building and populating it whereas the blog will be just one small news area, so I’d rather randomize the pages not the posts.

The function was written for the final suggestion on my 404 page - partly for users who have exhausted all other options, but mainly just for kicks :slight_smile:

ah ok, so you are using WP as an CMS versus a blog platform… :wink:
Doesn’t hurt to check.