1. Advertising
    y u no do it?

    Advertising (learn more)

    Advertise virtually anything here, with CPM banner ads, CPM email ads and CPC contextual links. You can target relevant areas of the site and show ads based on geographical location of the user if you wish.

    Starts at just $1 per CPM or $0.10 per CPC.

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

    Likes Received:
    Best Answers:
    Trophy Points:
    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);
    plog, Jan 30, 2009 IP
  3. ignitesystems

    ignitesystems Peon

    Likes Received:
    Best Answers:
    Trophy Points:
    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