SitePoint Sponsor

User Tag List

Results 1 to 9 of 9
  1. #1
    SitePoint Member
    Join Date
    Oct 2009
    Location
    Christchurch, NZ
    Posts
    5
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)

    Question 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.

    PHP Code:
    /*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

  2. #2
    Follow Me On Twitter: @djg gold trophysilver trophybronze trophy Dan Grossman's Avatar
    Join Date
    Aug 2000
    Location
    Philadephia, PA
    Posts
    20,578
    Mentioned
    1 Post(s)
    Tagged
    0 Thread(s)
    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.

    PHP Code:
    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(); 

  3. #3
    SitePoint Member
    Join Date
    Oct 2009
    Location
    Christchurch, NZ
    Posts
    5
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)

    Smile

    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)
    PHP Code:
    /* 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

  4. #4
    Follow Me On Twitter: @djg gold trophysilver trophybronze trophy Dan Grossman's Avatar
    Join Date
    Aug 2000
    Location
    Philadephia, PA
    Posts
    20,578
    Mentioned
    1 Post(s)
    Tagged
    0 Thread(s)
    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.

  5. #5
    SitePoint Addict Mal Curtis's Avatar
    Join Date
    Jul 2009
    Location
    New Zealand
    Posts
    327
    Mentioned
    14 Post(s)
    Tagged
    0 Thread(s)
    Hey Richard, how's your cousin?

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

    PHP Code:
    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(); 

  6. #6
    SitePoint Member
    Join Date
    Oct 2009
    Location
    Christchurch, NZ
    Posts
    5
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Thanks Mal :P

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

  7. #7
    SitePoint Wizard rguy84's Avatar
    Join Date
    Sep 2005
    Location
    Durham, NC
    Posts
    1,659
    Mentioned
    5 Post(s)
    Tagged
    0 Thread(s)
    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...
    Ryan B | My Blog | Twitter

  8. #8
    SitePoint Member
    Join Date
    Oct 2009
    Location
    Christchurch, NZ
    Posts
    5
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)

    Cool

    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

  9. #9
    SitePoint Wizard rguy84's Avatar
    Join Date
    Sep 2005
    Location
    Durham, NC
    Posts
    1,659
    Mentioned
    5 Post(s)
    Tagged
    0 Thread(s)
    ah ok, so you are using WP as an CMS versus a blog platform...
    Doesn't hurt to check.
    Ryan B | My Blog | Twitter


Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •