WordPress.org

Ready to get started?Download WordPress

Forums

[resolved] orderby=rand Not working on custom loops (2 posts)

  1. pineconez
    Member
    Posted 3 years ago #

    I am using two custom loops to display items from different tags in different areas of a "static" main page. I would like the content to be displayed randomly, something like a random blurb on the main page each time it is reloaded.

    <div id="loop1">
    	<?php query_posts('cat=6&showposts=1&orderby=rand'); ?>
    	<?php $posts = get_posts('category=6&numberposts=1&offset=0'); foreach ($posts as $post) : start_wp(); ?>
    	<?php the_content(); ?>
    	<?php endforeach; ?>
    </div>

    Both loops are exactly the same but use a different category. I know the loops work because the last post of each is displayed, the only problem is orderby=rand is not loading anything different on a reload.

    Any help with the issue would be appreciated. If you have questions please ask.

  2. pineconez
    Member
    Posted 3 years ago #

    I got it to work, for future reference, here's how you can have multiple random loops on a single wordpress page:

    <div id="loop1">
    	<?php query_posts($query_string . 'cat=5&showposts=1&orderby=rand'); ?>
    	<?php if ( have_posts() ) : while ( have_posts() ) : the_post(); ?>
    	<?php the_content(); ?>
    	<?php endwhile; ?>
    	<?php endif; ?>
    </div>

    * Note that I stripped everything from this loop and it only displays a single post, you are still able to add your own <?php the_title(); ?> etc.

    I assume you can have as many of these on your page as you have categories... Possibilities are endless! Hopefully someone finds this helpful down the road.

Topic Closed

This topic has been closed to new replies.

About this Topic