A WordPress Developer’s Best Friend

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.

See Also
black flat screen computer monitor

<?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!

Picture of Nathan Rice

Nathan Rice

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.

RECENT ARTICLES

TRENDING AROUND THE WEB

If you recognize these 8 signs, you married your best friend

If you recognize these 8 signs, you married your best friend

Global English Editing

Psychology says these 7 self-care habits are crucial for aging well

Psychology says these 7 self-care habits are crucial for aging well

Global English Editing

7 signs you have a highly appealing personality, says science

7 signs you have a highly appealing personality, says science

Global English Editing

If you wake up the same time every morning without an alarm, psychology says you probably exhibit these 8 traits

If you wake up the same time every morning without an alarm, psychology says you probably exhibit these 8 traits

Global English Editing

People who care more about ambiance than food when they dine out often display these 8 traits

People who care more about ambiance than food when they dine out often display these 8 traits

Global English Editing

8 signs you’re not just aging well — you’re becoming the best version of yourself

8 signs you’re not just aging well — you’re becoming the best version of yourself

Global English Editing