Quick Tip: WordPress Random Posts

Using a WordPress Custom Loop

Have you ever wanted to display random posts (portfolio items) in your sidebar similar to my site?

Well today I am going to quickly show you how this is done.

Create a Category

First of all you will to create a category to hold all your portfolio items. Then create your portfolio items as “posts” and place them in the category you just created.

Now you have the framework for which to begin pulling these posts in a random order.

Create a WordPress Custom Loop

Begin by opening up “sidebar.php” in your theme folder. We will be using a WordPress custom loop to achieve our goal.

$port_items = new WP_Query();
$port_items->query('showposts=3&category_name=portfolio&orderby=rand');

The info that is passed to the query is the important part.

showposts=3 : Only show 3 posts
category_name=portfolio : only show posts within the specified category
orderby=rand : show in random order

You would then proceed with the normal WordPress Loop and display the information however you need.

while($port_items->have_posts()):

// display here

endwhile;

This has been your quick tip today. What other tips would you like to learn about? Share them in the comments!

Be Sociable, Share!

Related posts:

  1. WordPress: More than just a blogging platform?
  2. WordPress 3.0 Custom Posts
  3. Excluding WordPress Categories from RSS Feed
  4. 50 WordPress Resources
  5. WordPress 2.7 is out!


Written by brenelz

Hello everyone, I'm Brenley Dueck or better known as Brenelz. I currently run my own business called Brenelz Web Solutions which focuses primary on winnipeg website design. The web technologies I most specialize in are CSS, jQuery, AJAX, PHP, and the MySQL database. Please make sure to follow me on twitter.

 

One Response to “Quick Tip: WordPress Random Posts”

  1. David Tiquet Says:

    September 7th, 2011 at 2:07 pm

    Thank you! i was trying to put my category post random, and with your help, i had get it!

    Regards!

Leave a Reply

XHTML: You can use these tags: <a href="" title=""> <abbr title=""> <acronym title=""> <b> <blockquote cite=""> <cite> <code> <del datetime=""> <em> <i> <q cite=""> <strike> <strong>

 
connect with me!