A WordPress Developer’s Best Friend

May 31, 2007 | By Nathan Rice | Filed Under Blog Design, Features, Guides and Tutorials

Have you ever wondered how some WordPress blogs have all those little sections of content? You know, one list that has the 5 most recent stories in a specific category. Another section has a “aside” story. Yet another list called “news” that has a list of stories, but they never get to the main part of the homepage.

This is sometimes known as a “magazine” or “newspaper” style homepage, and although it is a bit complicated to create, the common denominator is a handy little loop condition called “query_posts“.

I first discovered it, of course, by client request. A client wanted a homepage to have a “sideblog” in one of the sidebars that displayed the title and excerpt from all the posts he put in the “sideblog” category.

In fact, the same strategy is employed here on The Blog Herald. See how in the sidebar, directly under the ads, there is a list of news? Where does that come from? For that matter, where does the feature news on the left side come from?

query_posts — that’s where.

query_posts is a way to qualify the loop. It allows you to tell the look which articles to fetch. And it’s pretty straightforward too. Here are some common uses:

<?php query_posts(’cat=1′); ?>

Place that little blurb before your loop, and it will only display stories in category 1 (that would be the category ID, not the name).

<?php query_posts(’showposts=3′); ?>

This will output only 3 stories.

<?php query_posts(’category_name=Staff Home’); ?>

This one limits the loop to only output stories in the “Staff Home” category (this time, it uses the name, not the category ID).

And the possibilities are nearly endless.

So if you ever wanted to know how the big boys make their themes, there you go. Yes, it’s a bit anticlimactic, but a neat trick nevertheless. Over the next few weeks, I’ll be sharing some other tricks like this to make your WordPress theme super cool using query_posts. For more information on the query_posts qualifier, check out the codex. Lots of cool tips there!

Happy coding!


About the author: Nathan is a blogger and freelance web designer, specializing in WordPress theme development. He runs his own blog at NathanRice.net ranging in topics from web design, to WordPress tips, and is a contributer at Performancing, The Blog Herald, and WordPressThemes.com.



Comments

15 Responses to “A WordPress Developer’s Best Friend”

  1. Wordpress: “query_posts”, la requête à tout faire ! on June 5th, 2007 10:00 am

    […] viens de tomber sur un article très intéressant du Blog Herald qui explique comment fonctionne une condition importante du loop Wordpress, la […]

  2. Online Dating on June 6th, 2007 1:35 pm

    What wordpress design is this? I would like to get a copy of it. It’s a very clean look.

    Also, What tools are you using for this wordpress theme?

  3. game machine on June 7th, 2007 9:14 am

    Very Good!!

  4. Creating an Link Blog Within Your Blog : The Blog Herald on June 7th, 2007 11:47 am

    […] week, I introduced the query_posts loop qualifier, and this week I’m going to give you a practical use of the tag. For an […]

  5. Query_Posts and Your Homepage Design: Part 2 : The Blog Herald on June 29th, 2007 11:34 am

    […] from the latest entries in our blog to our homepage, we’re going to employ, once again, the WordPress developer’s best friend, […]

  6. My new WPMU Site » Blog Archive » Query_Posts and Your Homepage Design: Part 2 on July 18th, 2007 6:28 pm

    […] from the latest entries in our blog to our homepage, we’re going to employ, once again, the WordPress developer’s best friend, query_posts. […]

  7. Your title here » Blog Archive » The Journey in Building my Website on July 19th, 2007 3:11 pm

    […] Wordpress Developer Best Friend […]

  8. iDream » Post Topic » Welcome! on August 7th, 2007 1:41 am

    […] Wordpress Developer Best Friend […]

  9. thoughtstart » WordPress and the one and only loop on September 13th, 2007 1:48 am

    […] A WordPress Developer’s Best Friend : The Blog Herald […]

  10. Some new links for WP Developers « Soon to be the Biggest Blog on the Internet on March 21st, 2008 12:10 pm
  11. Weblog Tools Collection » Blog Archive » Define Your Own WordPress Loop Using WP_Query on April 13th, 2008 2:00 am

    […] One of the easiest ways to navigate and manipulate the loop is to use the function called query_posts. Nathan Rice calls it a WordPress developers best friend. […]

  12. Wp Wordpress » Blog Archive » Define Your Own WordPress Loop Using WP_Query on April 13th, 2008 3:01 am

    […] One of the easiest ways to navigate and manipulate the loop is to use the function called query_posts. Nathan Rice calls it a WordPress developers best friend. […]

  13. Define Your Own WordPress Loop Using WP_Query | BlogBroker24-7 on April 13th, 2008 3:06 am

    […] One of the easiest ways to navigate and manipulate the loop is to use the function called query_posts. Nathan Rice calls it a WordPress developers best friend. […]

  14. My New Blog » Blog Archive » Define Your Own WordPress Loop Using WP_Query on April 17th, 2008 5:56 am

    […] One of the easiest ways to navigate and manipulate the loop is to use the function called query_posts. Nathan Rice calls it a WordPress developers best friend. […]

  15. blog.rotracker.net » Blog Archive » Define Your Own WordPress Loop Using WP_Query on May 8th, 2008 4:06 am

    […] One of the easiest ways to navigate and manipulate the loop is to use the function called query_posts. Nathan Rice calls it a WordPress developers best friend. […]

Contributors

Bloggy Award

Latest Reviews

More at Blogosphere News

Submitted News

Our Blog Metrics

Recommended

Splashpress Media

Powered by WordPress | Designed by Brian Gardner
Copyright © 2003 - 2008 by The Blog Herald - All Rights Reserved