Best way to Rotate Posts

Discussion in 'WordPress' started by abusschaert, Dec 10, 2012.

  1. #1
    How do you rotate posts? So when you are on the main page that has all your posts, it is always changing, (rotating every 5 minutes).
     
    abusschaert, Dec 10, 2012 IP
  2. adbox

    adbox Well-Known Member

    Messages:
    906
    Likes Received:
    7
    Best Answers:
    0
    Trophy Points:
    155
    Digital Goods:
    1
    #2
    Without a plugin we would have to manually modify the homepage query.

    This could be added to your theme's functions.php file:

    
    <?php
    
    
    /**
     * Randomize front page post
     * 
     * @author Bill Erickson (modded by Hudson Atwell (@atwellpub)
     * @link http://www.billerickson.net/customize-the-wordpress-query/
     * @param object $query data
     *
     */
    add_action( 'pre_get_posts', 'frontpage_randomize_post' );
    function frontpage_randomize_post( $query ) {
    
    	if( $query->is_main_query() && $query->is_home() ) {
    		$query->set( 'order', 'RAND' );
                    $query->set( 'limit', '1' );
    	}
    
    }
    
    Code (markup):
    This may not work if caching is enabled.
     
    adbox, Dec 11, 2012 IP
  3. zimzalabim

    zimzalabim Active Member

    Messages:
    102
    Likes Received:
    0
    Best Answers:
    0
    Trophy Points:
    51
    #3
    zimzalabim, Dec 11, 2012 IP