How to add a Random Post button in wordpress

How to add a Random Post button in wordpress

In this post I will show you 2 methods of how to create a Random Post button in wordpress. Both are short snippets and easy to implement.

Method #1: custom page template

First create a custom page template random-post-page.php (see how to create a page template) and add the following codes in it.

Upload this random-post-page.php file to your theme directory and then create a new page choosing the page template we just created Random Post.

That’s it. You can now create a button in your navbar or anywhere for users to click on that page and he/she will be redirected to a random post.

Method #2: Code snippet

First add following in your functions.php.

That’s it and you are done. Now create a button that links to or and the snippet above will take care of the rest. Click below link for a test.

Thanks tieke for the snippet #1 and wpbeginner for snippet #2.


Author: Leo

My name is Leo who's behind this blog. I've been working with wordpress since 2010. I'll write about themes, plugins, tutorials and all about wordpress here. Hope you find it useful!

Leave a Reply

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