Reputation: 6895
I haven't worked much with wordpress, what I am trying to do is have a page that displays random posts.
It would be similar to my main page where the latest posts are shown but it would display random posts every time the page is refreshed. If the main page is at http://example.com i want my random page to be at http://example.com/random
how to do it?
Upvotes: 2
Views: 2908
Reputation: 30071
permalinks
for you wordpress installation by visiting http://example.com/wp-admin/options-permalink.php
, If you would like the title of the post to be the last url segment you would select Custom Structure
and set this value to %post_name%
Random
and select your newly created page template under Page Attributes
in the pages edit screen.Then in your page template you would do something like this as Filip suggested:
<ul>
<?php
$args = array('posts_per_page' => 5, 'orderby' => 'rand');
$rand_posts = get_posts($args);
foreach ($rand_posts as $post) : ?>
<li><a href="<?php the_permalink(); ?>"><?php the_title(); ?></a></li>
<?php endforeach; ?>
</ul>
Upvotes: 1
Reputation: 197
Try This ...
1.First a create a custom page template. Name it as random post or a name of your choice!
2.Open the page and remove the default wp loop and Paste the code below
3.To change the no of post change the number ‘1’ to your choice!
<?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; ?>
source: http://www.yengkokpam.com/displays-random-posts-in-a-page/
Upvotes: 1
Reputation: 21
You can use this code to display random posts on your page.
<?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; ?>
Upvotes: 0
Reputation: 2522
The orderby
argument to get_posts
accepts the value rand
.
<ul>
<?php
$args = array( 'numberposts' => 5, 'orderby' => 'rand' );
$rand_posts = get_posts( $args );
foreach( $rand_posts as $post ) : ?>
<li><a href="<?php the_permalink(); ?>"><?php the_title(); ?></a></li>
<?php endforeach; ?>
</ul>
More info in the documentation: http://codex.wordpress.org/Template_Tags/get_posts#Random_posts
Upvotes: 3