Reputation: 51
I'm using the basic loop code in a taxonomy archive (artists) and I was wondering how you can set the loop to show posts in random order ('orderby'=>'rand') it doesn't seem to work when I add the array? Any help would be great!
<?php
// Start the Loop.
while ( have_posts() ) : the_post();
/*
* Include the post format-specific template for the content. If you want to
* use this in a child theme, then include a file called called content-___.php
* (where ___ is the post format) and that will be used instead.
*/
array ( 'orderby' => 'RAND' );
get_template_part( 'content', get_post_format() );
endwhile;
// Previous/next page navigation.
twentyfourteen_paging_nav();
else :
// If no content, include the "No posts found" template.
get_template_part( 'content', 'none' );
endif;
?>
Upvotes: 4
Views: 14671
Reputation: 25
Try this:
<?php
$args = array(
'orderby' => 'rand'
);
$query = new WP_Query($args);
if (have_posts()) {
while ( $query->have_posts() ) : $query->the_post();
get_template_part( 'content', get_post_format() );
endwhile;
// Previous/next page navigation.
twentyfourteen_paging_nav();
else :
// If no content, include the "No posts found" template.
get_template_part( 'content', 'none' );
endif;
?>
Upvotes: -1
Reputation: 146
query_posts(array( 'showposts' => 6, 'orderby' => 'rand', 'category_name' => 'News' //You can insert any category name ));
Upvotes: 1
Reputation: 5211
<?php
$query = new WP_Query( array ( 'orderby' => 'rand', 'posts_per_page' => '-1' ) );
if( $query->have_posts() ):
// Start the Loop.
while ( $query->have_posts() ) : $query->the_post();
/*
* Include the post format-specific template for the content. If you want to
* use this in a child theme, then include a file called called content-___.php
* (where ___ is the post format) and that will be used instead.
*/
get_template_part( 'content', get_post_format() );
endwhile;
// Previous/next page navigation.
twentyfourteen_paging_nav();
else :
// If no content, include the "No posts found" template.
get_template_part( 'content', 'none' );
endif;
?>
Upvotes: 10
Reputation: 14381
You have two ways of doing it. The first way is not the best way, but it may be simpler for you to understand:
Using WP_Query
<?php
$args = array(
'orderby' => 'random'
);
$query = new WP_Query( $args );
if( $query->have_posts() ):
// Start the Loop.
while ( $query->have_posts() ) : $query->the_post();
get_template_part( 'content', get_post_format() );
endwhile;
// Previous/next page navigation.
twentyfourteen_paging_nav();
else :
// If no content, include the "No posts found" template.
get_template_part( 'content', 'none' );
endif;
Here, we'll be using a custom WP_Query
object and orderby
to get random posts.
Using pre_get_posts
The best way to do it is by using the pre_get_post
action to modify the page output automatically. You might need some more coding though.
Upvotes: -2
Reputation: 11
Nice Question first !
You can do that with simple using function of PHP. http://www.php.net/manual/en/function.shuffle.php
Follow below step:
Please ask me after implementation if any query.
Thanks !
Upvotes: -1