We mentioned a plugin that allows you to have Stumbleupon like Browsing system in WordPress. The problem with that plugin is that it redirects a user to the actual URL which can create some user end issues such as page not loading, redirect error etc. In this post we will show you how you can create a page that displays a random post in WordPress, so you can simply send your user to that page and they can read a new post every time they refresh the page.
Step 1: of this tutorial is to create a custom page template in WordPress.
Step 2: Add the following code in your custom page template:
<?php
query_posts(array('orderby' => 'rand', 'showposts' => 1));
if (have_posts()) :
while (have_posts()) : the_post(); ?>
<h1><a href="<?php the_permalink() ?>"><?php the_title(); ?></a></h1>
<?php the_content(); ?>
<?php endwhile;
endif; ?>
That’s it. Depending on the name that you choose for the page, you can now create a button in your navbar for users to click on that page and view a random post.
Live Example
(Simply refresh a page to see a new post)
Solution:
There wasn't an extra closing tag for a div as indicated in validation. The openings and closings all equaled out.
The validation check generated that error notice due to a previous issue in the page with a call for an Adsense ad block. I had commented it out --perhaps not completely -- because it was still generating a closing tag for a script that was not open to begin with. That caused the validation to erroneously indicate the extra div closing.
It took a while to track it back, but it all works fine now in any browser after deleting that commented out Adsense call line line (didn't need it anyway). The page validates much better now with no div error remaining.
Thanks again for your help. You steered me in the right direction.
:-)
John
- spam
- offensive
- disagree
- off topic
Like