Going to a Random Page in Wordpress

Discussion in 'Programming' started by MelogKnaj, Jan 30, 2009.

  1. #1
    I have a wordpress blog and I want a simple function that allows me to click on a link and take me to a random page on the site. The page url format is http://example.com/page/2/, so I basically need to know how to insert a random number in place of that 2. It also needs to be within the number of pages I have on the site. Any idea how to do this in php/html?
     
    MelogKnaj, Jan 30, 2009 IP
  2. plog

    plog Peon

    Messages:
    298
    Likes Received:
    11
    Best Answers:
    1
    Trophy Points:
    0
    #2
    Try this. It randomly pulls a parent post ID from the post table and returns just a number.

    
    
    function get_randompost()
        // randomly pulls a post number from the database
        // returns an integer
        // a database connection must be made prior to executing this function
        {$sql="SELECT ID FROM wp_posts WHERE post_parent=0 ORDER BY RAND() LIMIT 1;"
    	return mysql_result(mysql_query($sql), 0, 0);
        }
    
    PHP:
     
    plog, Jan 30, 2009 IP
  3. ignitesystems

    ignitesystems Peon

    Messages:
    26
    Likes Received:
    1
    Best Answers:
    0
    Trophy Points:
    0
    #3
    PHP has a method for random numbers:

    echo rand(5, 15);

    This would output a random number between 5 and 15.

    Just do:

    $randnum = rand(5, 15);
    echo "<a href='myblog.com/page/".$randnum."'>Random Link</a>";
     
    ignitesystems, Feb 4, 2009 IP