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.
random-post-page.php file to your theme directory and then create a new page choosing the page template we just created
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
That’s it and you are done. Now create a button that links to
yourdomain.com/index.php?random=1 and the snippet above will take care of the rest. Click below link for a test.