Skip to:
Content
Pages
Categories
Search
Top
Bottom

Reply To: Random Topic Link

@robkk

Moderator

@robin-w, @joejozwowski

i found the code to create a random post link that you can use as a reference , when creating a random topic link

got it from http://cazue.com/articles/wordpress-create-a-random-post-button-2013

add_action('init','random_post');
function random_post() {
       global $wp;
       $wp->add_query_var('random');
       add_rewrite_rule('random/?$', 'index.php?random=1', 'top');
}
 
add_action('template_redirect','random_template');
function random_template() {
       if (get_query_var('random') == 1) {
               $posts = get_posts('post_type=post&orderby=rand&numberposts=1');
               foreach($posts as $post) {
                       $link = get_permalink($post);
               }
               wp_redirect($link,307);
               exit;
       }
}

link to show

<a href="/?random=1">Random Post</a>

creating a menu link , idk how to bring that link up in the menu

Skip to toolbar