Last active
August 29, 2015 14:07
-
-
Save fovoc/b21144f83e76ba02476b to your computer and use it in GitHub Desktop.
Random Post Button
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| <?php | |
| 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; | |
| } | |
| } |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment