functions.php

<?php function random_post() { global $wpdb; $arr = $wpdb->get_results("SELECT ID FROM wp_posts WHERE post_type='post' and post_status='publish' ","ARRAY_A"); $curr_id = get_the_ID(); $numbr = array_rand($arr); if($arr[$numbr][ID] == $curr_id){ $numbr = array_rand($arr); } $new_id = $arr[$numbr][ID]; $postlink = get_permalink($new_id); return "<a href='" . $postlink . "' title='" . get_the_title($new_id) . "'>Random Post</a>"; }

Be the first to comment

You can use [html][/html], [css][/css], [php][/php] and more to embed the code. Urls are automatically hyperlinked. Line breaks and paragraphs are automatically generated.