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?
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:
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>";