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

    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.

    PHP:
    1.  
    2.  
    3. function get_randompost()
    4.     // randomly pulls a post number from the database
    5.     // returns an integer
    6.     // a database connection must be made prior to executing this function
    7.     {$sql="SELECT ID FROM wp_posts WHERE post_parent=0 ORDER BY RAND() LIMIT 1;"
    8.     return mysql_result(mysql_query($sql), 0, 0);
    9.     }
    10.  
     
    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