WordPress.org

Ready to get started?Download WordPress

Forums

random new post (2 posts)

  1. miniCarl
    Member
    Posted 3 years ago #

    Ok, a question about the loop.

    I have my category "news", with 10 posts.
    I want the loop to find the latest 3, and display only 1 of them (at random)

    I guess i could do something like this...

    $query = WP_Query('category_name=news&orderby=date&posts_per_page=3');
    $rand = rand(1, 3);
    $count = 1;
    while($query->have_posts()):
    	if($rand != $count){
    		$count++;
    		continue;
    	}
    	// Display post here
    endwhile;

    ...but its not pretty.

    Any sugestions, can i do this just with the right loop arguments?

Topic Closed

This topic has been closed to new replies.

About this Topic