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
person holding black iphone 5

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

7 signs you’re in a relationship with someone who lacks emotional depth

7 signs you’re in a relationship with someone who lacks emotional depth

Baseline

10 personality traits of men who show true loyalty in a relationship

10 personality traits of men who show true loyalty in a relationship

Global English Editing

10 signs you’re giving way too much and your partner is taking advantage of it

10 signs you’re giving way too much and your partner is taking advantage of it

Small Business Bonfire

4 zodiac signs who are definitely on the right path in life

4 zodiac signs who are definitely on the right path in life

Baseline

7 clever ways to show a master manipulator you won’t play their games

7 clever ways to show a master manipulator you won’t play their games

Small Business Bonfire

10 subtle signs you are actually an extremely likable person

10 subtle signs you are actually an extremely likable person

Global English Editing