WordPress.org

Ready to get started?Download WordPress

Forums

[resolved] Random order of posts at home (2 posts)

  1. Julian
    Member
    Posted 2 years ago #

    Hi,

    I would like set my posts at a random order instead of the date (ignoring the date).

    This is the code from my theme:

    <?php if (have_posts()) : while (have_posts()) : the_post(); if( $post->ID == $do_not_duplicate ) continue; update_post_caches($posts); $i++; ?>
    <div class="span-8 post-<?php the_ID(); ?><?php if ($i == 3) { ?> last<?php } ?>">
    <h6 class="archive-header">" rel="bookmark" title="<?php the_title_attribute(); ?>"><?php the_title() ?></h6>
    <?php get_the_image( array( 'custom_key' => array( 'thumbnail' ), 'default_size' => 'thumbnail', 'width' => '310', 'height' => '150' ) ); ?>
    <?php the_excerpt(); ?>
    <p class="postmetadata"><p class="postmetadata"><?php the_tags(); ?></p>
    </div>
    <?php if ($i == 3) { ?><div class="archive-stack clear"></div><?php $i = 0; } ?>
    <?php endwhile; endif; ?>

  2. keesiemeijer
    moderator
    Posted 2 years ago #

    If this is for your homepage, put this in your theme's functions.php:

    function random_home_posts($query) {
       if($query->is_main_query()){
          if ($query->is_home()) {
            $query->set('orderby', 'rand');
          }
        }
    
    };
    add_action('pre_get_posts','random_home_posts');

    or try it with a query on the loop:
    http://codex.wordpress.org/Function_Reference/query_posts
    http://codex.wordpress.org/Class_Reference/WP_Query#Order_.26_Orderby_Parameters

Topic Closed

This topic has been closed to new replies.

About this Topic