Viewing 4 replies - 1 through 4 (of 4 total)
  • I assume you will use query_posts on your page to retrieve the posts. For the random loop, use a query similar to this:

    $args = array(
       'orderby' => 'rand',
       'posts_per_page' => 5
    );
    query_posts($args);
    if (have_posts()) . . .

    This will show 5 random posts.

    Edit: oops – samboll beat me to it.

    Thread Starter refulez

    (@refulez)

    i found a solution `<?php query_posts(array(
    ‘orderby’ => ‘rand’
    ));
    if (have_posts()) : while (have_posts()) : the_post();?>`
    is there a way to excude already seen posts?
    and any idea on how to paginate it ?
    i have something in place <?php if(function_exists('wp_pagenavi')) { wp_pagenavi(); } ?></div> but it only pagintes the mainpage not the page template i created for the random posts
    the pagenavi function in integrated in the theme it looks like this

    function wp_pagenavi($before = '', $after = '', $prelabel = '', $nxtlabel = '', $pages_to_show = 5, $always_show = true) {
    
    	global $request, $posts_per_page, $wpdb, $paged;
    
    	if(empty($prelabel)) {
    
    		$prelabel  = '<strong>&laquo;</strong>';
    
    	}
    
    	if(empty($nxtlabel)) {
    
    		$nxtlabel = '<strong>&raquo;</strong>';
    
    	}
    
    	$half_pages_to_show = round($pages_to_show/2);
    
    	if (!is_single()) {
    
    		if(!is_category()) {
    
    			preg_match('#FROM\s(.*)\sORDER BY#siU', $request, $matches);		
    
    		} else {
    
    			preg_match('#FROM\s(.*)\sGROUP BY#siU', $request, $matches);		
    
    		}
    
    		$fromwhere = $matches[1];
    
    		$numposts = $wpdb->get_var("SELECT COUNT(DISTINCT ID) FROM $fromwhere");
    
    		$max_page = ceil($numposts /$posts_per_page);
    
    		if(empty($paged)) {
    
    			$paged = 1;
    
    		}
    
    		if($max_page > 1 || $always_show) {
    
    			echo "$before <div class='Nav'><span>Page $paged of $max_page</span>";
    
    			if ($paged >= ($pages_to_show-1)) {
    
    				echo '<a href="'.get_pagenum_link().'">&laquo; First</a> ... ';
    
    			}
    
    			previous_posts_link($prelabel);
    
    			for($i = $paged - $half_pages_to_show; $i  <= $paged + $half_pages_to_show; $i++) {
    
    				if ($i >= 1 && $i <= $max_page) {
    
    					if($i == $paged) {
    
    						echo "<strong class='on'>$i</strong>";
    
    					} else {
    
    						echo ' <a href="'.get_pagenum_link($i).'">'.$i.'</a> ';
    
    					}
    
    				}
    
    			}
    
    			next_posts_link($nxtlabel, $max_page);
    
    			if (($paged+$half_pages_to_show) < ($max_page)) {
    
    				echo ' ... <a href="'.get_pagenum_link($max_page).'">Last &raquo;</a>';
    
    			}
    
    			echo "<div class='NavEnd'></div></div> $after";
    
    		}
    
    	}
    
    }
    ?>

    Hi,

    Check with this plugin:

    http://ww.wp.xz.cn/extend/plugins/random-featured-post-plugin/

    Thanks,

    Shane G.

Viewing 4 replies - 1 through 4 (of 4 total)

The topic ‘Random posts on a page’ is closed to new replies.