I'm trying to randomize the posts shown on my homepage, which are being pulled from specific categories. Take a look, and let me know if you need more info. https://laxaxis.com
Fahad Murtaza answers:
Place
query_posts(array('orderby' => 'rand', 'showposts' => 1));
before your
if (have_posts()) :
and replace the 1 is showposts by 1.
Fahad Murtaza comments:
Also post your code for us to look at.
Patrick MacAdams comments:
<?php
echo "<div class='wrapper' style='height:780px;'><br /><h1>TOURNAMENTS</h1><br />";
$count1 = 0;
// The Query for tournament cat id 3
$the_query = new WP_Query("cat=3&showposts=11");
// The Loop
query_posts(array('orderby' => 'rand', 'showposts' => 1));
while ( $the_query->have_posts() ) : $the_query->the_post();
if($count1==0){
echo cooolzine_index_toparticle($post);
}else{
echo cooolzine_index_boxarticle($post);
}
$count1++;
endwhile;
// Reset Post Data
wp_reset_postdata();
if (get_option(get_current_theme().'_adside') == 'true') {
if (function_exists('dynamic_sidebar') && dynamic_sidebar('Sidebar Homepage1')) {}
} // Sidebar Homepage 1
$cat_link1 = get_category_link(3);
echo "</div><h2><a href='$cat_link1' style='float:right;'>See More Tournaments...</a></h2><br/>";
echo "<div class='wrapper' style='height:420px;'><br /><h1>CAMPS</h1><br />";
$count2 = 0;
// The Query for Camps cat id 4
$the_query = new WP_Query("cat=4&showposts=5");
// The Loop
while ( $the_query->have_posts() ) : $the_query->the_post();
if($count2==0){
echo cooolzine_index_toparticle($post);
}else{
echo cooolzine_index_boxarticle($post);
}
$count2++;
endwhile;
// Reset Post Data
wp_reset_postdata();
if (get_option(get_current_theme().'_adside') == 'true') {
if (function_exists('dynamic_sidebar') && dynamic_sidebar('Sidebar Homepage2')) {}
} // Sidebar Homepage 2
$cat_link2 = get_category_link(4);
echo "</div><h2><a href='$cat_link2' style='float:right;'>See More Camps...</a></h2>";
echo "<div class='wrapper' style='height:420px;'><br /><h1>CLUBS</h1><br />";
$count3 = 0;
// The Query for clubs cat id 6
$the_query = new WP_Query("cat=6&showposts=5");
// The Loop
while ( $the_query->have_posts() ) : $the_query->the_post();
if($count3==0){
echo cooolzine_index_toparticle($post);
}else{
echo cooolzine_index_boxarticle($post);
}
$count3++;
endwhile;
// Reset Post Data
wp_reset_postdata();
if (get_option(get_current_theme().'_adside') == 'true') {
if (function_exists('dynamic_sidebar') && dynamic_sidebar('Sidebar Homepage3')) {}
} // Sidebar Homepage 3
$cat_link3 = get_category_link(6);
echo "</div><h2><a href='$cat_link3' style='float:right;'>See More Clubs...</a></h2>";
echo "<div class='wrapper' style='height:420px;'><br /><h1>CLINICS</h1><br />";
$count4 = 0;
// The Query for Clinics cat id 5
$the_query = new WP_Query("cat=5&showposts=5");
// The Loop
while ( $the_query->have_posts() ) : $the_query->the_post();
if($count4==0){
echo cooolzine_index_toparticle($post);
}else{
echo cooolzine_index_boxarticle($post);
}
$count4++;
endwhile;
// Reset Post Data
wp_reset_postdata();
if (get_option(get_current_theme().'_adside') == 'true') {
if (function_exists('dynamic_sidebar') && dynamic_sidebar('Sidebar Homepage4')) {}
} // Sidebar Homepage 4
$cat_link4 = get_category_link(5);
echo "</div><h2><a href='$cat_link4' style='float:right;'>See More Clinics...</a></h2>";
echo "<div class='wrapper' style='height:420px;'><br /><h1>LEAGUES</h1><br />";
$count5 = 0;
// The Query for Leagues cat id 6
$the_query = new WP_Query("cat=44&showposts=1");
// The Loop
while ( $the_query->have_posts() ) : $the_query->the_post();
if($count5==0){
echo cooolzine_index_toparticle($post);
}else{
echo cooolzine_index_boxarticle($post);
}
$count5++;
endwhile;
// Reset Post Data
wp_reset_postdata();
if (get_option(get_current_theme().'_adside') == 'true') {
if (function_exists('dynamic_sidebar') && dynamic_sidebar('Sidebar Homepage5')) {}
} // Sidebar Homepage 5
$cat_link5 = get_category_link(44);
echo "</div><h2><a href='$cat_link5' style='float:right;'>See More Leagues...</a></h2>";
echo "<div class='wrapper' style='height:420px;'><br /><h1>SCHOOLS</h1><br />";
$count6 = 0;
// The Query for schools cat id
$the_query = new WP_Query("cat=7&showposts=1");
// The Loop
while ( $the_query->have_posts() ) : $the_query->the_post();
if($count6==0){
echo cooolzine_index_toparticle($post);
}else{
echo cooolzine_index_boxarticle($post);
}
$count6++;
endwhile;
// Reset Post Data
wp_reset_postdata();
if (get_option(get_current_theme().'_adside') == 'true') {
if (function_exists('dynamic_sidebar') && dynamic_sidebar('Sidebar Homepage6')) {}
} // Sidebar Homepage 6
$cat_link6 = get_category_link(7);
echo "</div><h2><a href='$cat_link6' style='float:right;'>See More Schools...</a></h2>";
?>
Fahad Murtaza comments:
Your new code, please try it
<?php
echo "<div class='wrapper' style='height:780px;'><br /><h1>TOURNAMENTS</h1><br />";
$count1 = 0;
// The Query for tournament cat id 3
$args = array(
'cat' => 3,
'ignore_sticky_posts' => 1,
'post__not_in' => $sticky,
'paged' => $paged,
'orderby' => 'rand',
'order' => 'DESC'
'showposts'=>'1'
);
$the_query = new WP_Query($args);
// The Loop
while ( $the_query->have_posts() ) : $the_query->the_post();
if($count1==0){
echo cooolzine_index_toparticle($post);
}else{
echo cooolzine_index_boxarticle($post);
}
$count1++;
endwhile;
// Reset Post Data
wp_reset_postdata();
if (get_option(get_current_theme().'_adside') == 'true') {
if (function_exists('dynamic_sidebar') && dynamic_sidebar('Sidebar Homepage1')) {}
} // Sidebar Homepage 1
$cat_link1 = get_category_link(3);
echo "</div><h2><a href='$cat_link1' style='float:right;'>See More Tournaments...</a></h2><br/>";
echo "<div class='wrapper' style='height:420px;'><br /><h1>CAMPS</h1><br />";
$count2 = 0;
// The Query for Camps cat id 4
$args2 = array(
'cat' => 4,
'ignore_sticky_posts' => 1,
'post__not_in' => $sticky,
'paged' => $paged,
'orderby' => 'rand',
'order' => 'DESC',
);
$the_query = new WP_Query($args);
// The Loop
while ( $the_query->have_posts() ) : $the_query->the_post();
if($count2==0){
echo cooolzine_index_toparticle($post);
}else{
echo cooolzine_index_boxarticle($post);
}
$count2++;
endwhile;
// Reset Post Data
wp_reset_postdata();
if (get_option(get_current_theme().'_adside') == 'true') {
if (function_exists('dynamic_sidebar') && dynamic_sidebar('Sidebar Homepage2')) {}
} // Sidebar Homepage 2
$cat_link2 = get_category_link(4);
echo "</div><h2><a href='$cat_link2' style='float:right;'>See More Camps...</a></h2>";
echo "<div class='wrapper' style='height:420px;'><br /><h1>CLUBS</h1><br />";
$count3 = 0;
// The Query for clubs cat id 6
$args3 = array(
'cat' => 6,
'ignore_sticky_posts' => 1,
'post__not_in' => $sticky,
'paged' => $paged,
'orderby' => 'rand',
'order' => 'DESC',
'showposts' => 5
);
$the_query = new WP_Query($args3);
// The Loop
while ( $the_query->have_posts() ) : $the_query->the_post();
if($count3==0){
echo cooolzine_index_toparticle($post);
}else{
echo cooolzine_index_boxarticle($post);
}
$count3++;
endwhile;
// Reset Post Data
wp_reset_postdata();
if (get_option(get_current_theme().'_adside') == 'true') {
if (function_exists('dynamic_sidebar') && dynamic_sidebar('Sidebar Homepage3')) {}
} // Sidebar Homepage 3
$cat_link3 = get_category_link(6);
echo "</div><h2><a href='$cat_link3' style='float:right;'>See More Clubs...</a></h2>";
echo "<div class='wrapper' style='height:420px;'><br /><h1>CLINICS</h1><br />";
$count4 = 0;
// The Query for Clinics cat id 5
$args4 = array(
'cat' => 5,
'ignore_sticky_posts' => 1,
'post__not_in' => $sticky,
'paged' => $paged,
'orderby' => 'rand',
'order' => 'DESC',
'showposts' => 5
);
$the_query = new WP_Query($args4);
// The Loop
while ( $the_query->have_posts() ) : $the_query->the_post();
if($count4==0){
echo cooolzine_index_toparticle($post);
}else{
echo cooolzine_index_boxarticle($post);
}
$count4++;
endwhile;
// Reset Post Data
wp_reset_postdata();
if (get_option(get_current_theme().'_adside') == 'true') {
if (function_exists('dynamic_sidebar') && dynamic_sidebar('Sidebar Homepage4')) {}
} // Sidebar Homepage 4
$cat_link4 = get_category_link(5);
echo "</div><h2><a href='$cat_link4' style='float:right;'>See More Clinics...</a></h2>";
echo "<div class='wrapper' style='height:420px;'><br /><h1>LEAGUES</h1><br />";
$count5 = 0;
// The Query for Leagues cat id 6
$args5 = array(
'cat' => 44,
'ignore_sticky_posts' => 1,
'post__not_in' => $sticky,
'paged' => $paged,
'orderby' => 'rand',
'order' => 'DESC',
'showposts' => 1
);
//$the_query = new WP_Query("cat=44&showposts=1");
$the_query = new WP_Query($args5);
// The Loop
while ( $the_query->have_posts() ) : $the_query->the_post();
if($count5==0){
echo cooolzine_index_toparticle($post);
}else{
echo cooolzine_index_boxarticle($post);
}
$count5++;
endwhile;
// Reset Post Data
wp_reset_postdata();
if (get_option(get_current_theme().'_adside') == 'true') {
if (function_exists('dynamic_sidebar') && dynamic_sidebar('Sidebar Homepage5')) {}
} // Sidebar Homepage 5
$cat_link5 = get_category_link(44);
echo "</div><h2><a href='$cat_link5' style='float:right;'>See More Leagues...</a></h2>";
echo "<div class='wrapper' style='height:420px;'><br /><h1>SCHOOLS</h1><br />";
$count6 = 0;
// The Query for schools cat id
$args5 = array(
'cat' => 7,
'ignore_sticky_posts' => 1,
'post__not_in' => $sticky,
'paged' => $paged,
'orderby' => 'rand',
'order' => 'DESC',
'showposts' => 1
);
//$the_query = new WP_Query("cat=7&showposts=1");
$the_query = new WP_Query($args5);
// The Loop
while ( $the_query->have_posts() ) : $the_query->the_post();
if($count6==0){
echo cooolzine_index_toparticle($post);
}else{
echo cooolzine_index_boxarticle($post);
}
$count6++;
endwhile;
// Reset Post Data
wp_reset_postdata();
if (get_option(get_current_theme().'_adside') == 'true') {
if (function_exists('dynamic_sidebar') && dynamic_sidebar('Sidebar Homepage6')) {}
} // Sidebar Homepage 6
$cat_link6 = get_category_link(7);
echo "</div><h2><a href='$cat_link6' style='float:right;'>See More Schools...</a></h2>";
?>
Julio Potier answers:
Hello
You can make a query_post with shiffle like this :
<?php shuffle(query_posts('author=1')); ?>
or random
<?php query_posts('author=1&orderby=rand'); ?>
or other random
<?php query_posts(array('author' => '1', 'orderby' => 'rand')); ?>
Done !
Monster Coder answers:
check this api doc:
http://codex.wordpress.org/Template_Tags/get_posts
if you write
<blockquote> 'orderby' => 'rand',</blockquote>
it should random posts
Marko Nikolic answers:
You don't need to randomize it by order, but by category, right?
Remove cat=xx& from code, from place you want to randomize it.
xx = number of category.