Connect with us

WP Stuff

How to Add a Random Post Button to WordPress

As webmasters, we all understand the importance of reducing our bounce rate and keeping our visitors on our website for longer. Using related and random posts plugins is one way to do that. I am a big fan of random posts plugins as they are easy to create and can be coded to put less load on your server. You can do this by writing a plugin or modifying your functions code. Here is what we want:

Currently trending WordPress plugins

A button that, when clicked, redirects users to a random post from your site and opens it in a new window. It also covers the entire width of your pages.

Here is the code I am using:

The idea here is simple: the ‘get_posts’ function to fetch a random post from the WordPress database. The `orderby` parameter is set to ‘rand’, so a random post is selected every time. I am fetching ‘ids’  for performance optimization. In the next section, you can see that I am using _blank as target to open links in a new window. The next function targets every posts and adds the button to the bottom of the page.

Here is what the plugin looks like:

This is a pretty simple script but encourage your visitors to see more posts on your site before leaving. How would you improve this?

Continue Reading
You may want to check out:
*some of our articles and lists include affiliate links to fund our operations (e.g. Amazon, Elegant Themes, Envato). Please read our disclaimer on how we fund Exxponent.
You may also like...
Click to comment

Leave a Reply

Your email address will not be published. Required fields are marked *

More in WP Stuff

To Top