Random post and Static post on frontpage
-
On the frontpage I want 7 posts which consists out of 1 random post on the top of the page and 6 static posts underneath it ordered like it standard is (ordered on the post that is last added).
I tried to add
[code]
<div id="random-post">
<?php if ( have_posts() ) : ?>
<?php query_posts(array('orderby' => 'rand', 'showposts' => 1)); ?>
<?php while ( have_posts() ) : the_post(); ?>
<?php get_template_part( 'content', get_post_format() ); ?>
<?php endwhile; ?>
<?php endif; ?>
</div>
[/code]
into the content.php file and the index.php file. I also tried to create a random-post.php file with the code above in it, and include this page into content.php and index.php. The thing what happens now is that the whole page only shows 2 posts which are both random and not 1 random and 6 static posts.Can someone give me some advice about what I`m doing wrong or what code to use to get my idea working?
-
According to this forum topic you cannot display a random post on your home page. You can, however, use a link, that when clicked will take readers to a random post each time.
Try this: Create a new post. If available in your theme, I’d suggest using the Aside or Link format so the title won’t show on the home page, but that doesn’t really matter. You can title it “Random Post” and in the text area also type “Random Post”. Use the link-tool to add the link “http://yourblogname.wordpress.com/?random” to the text. This way, every time someone clicks on “Random Post” they’ll be taken there. Set the post to sticky (under visibility in the publish box in the editor) and publish it.
Set the rest of the static posts you want to appear to sticky as well (you can also do this with the quick-edit feature on the “All Posts” page in the dashboard. The dates/times will determine their order, so just play around with that. Remember, most recent will appear first.
Lastly, under Settings > Reading in the dashboard, specify that your blog pages must show only 7 posts at a time and make sure endless scrolling is deactivated. This way the front page will only display these seven sticky posts, but the “older posts” link will still appear at the bottom allowing them to scroll further through your posts.
Hope this helps.
- The topic ‘Random post and Static post on frontpage’ is closed to new replies.