Create a random articles explore button on WordPress blog

Create a random articles explore button on WordPress blog

shuffle-article

You might have noticed that 9GAG.com uses a small Shuffle button which will take you to the random Images,Likewise WPBeginner.com also had a little Explore button on Navigation menu to randomly explore their contents.So in this post I have explained how to make random article explore button on WordPress.

Before beginning the lets analyze the advantage of using explore button.

1. Increases the Pageviews per user.

2. Useful to show up your old archive articles.

First of all find your Current themes Function.php and add the following code.If you use Thesis theme then you can paste this code on custom_functions.php

add_action('init','random_add_rewrite');
function random_add_rewrite() {
       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;
       }
}

Now if you go to  http://www.yourwebsite.com/index.php?random=1 (replace www.yourwebsite.com with our website name) you will be redirected to random article in your blog

You can use it with a Explore button to redirect the users like one you will see at the end of this post,the code I have used to do this
<div style="text-align:center;">
<a href="http://techiestuffs.com/index.php?random=1">
<img src="http://techiestuffs.com/wp-content/uploads/2012/06/explore-stuffs.png" alt="Explore Techiestuffs"/>
</a></div>

You can use your own Image and you can place this code on your sidebar using a Text widget.

You can Also display Explore button on your navigation menu,

random-article

Use your random article url in URL field and Give some suitable name to Navigation Label.Title attribute is the text displays when you hover over the menu item so give some brief title to it also.

Comment back if you have any difficulties in implementing this code.

About author

No Comments

forum No comments yet

You can be first to leave a comment

Write a comment