Query_Posts and Your Homepage Design

WordPress has a fantastic little loop qualifier. I’ve written on it for the last 2 weeks, and this week I want to go a little deeper, moving more toward the direction of using WordPress as a real content management system using the built in page feature that WordPress added a while back.

Although WP also recently added the ability to use any page as your homepage, most people running a business want a bit more complicated homepage. For instance, I’ve had clients that wanted to have both the content of a page, and the latest 3 blog entries on the homepage. Now, you may want a homepage that is more or less complicated than that, but you can take the principles outlined here and apply them to you own homepage design.

The Page Content

As you may remember, we can use the query_posts to allow the loop to pull just about any kind of information you can imagine! In this case, we’re going to use it to pull the information from a particular page. We’ll name it “homepage”. The first thing to do is actually create the content and post slug for the homepage. Go into your WordPress admin panel and click “Write” –> “Page”. Type the title and body of your content (whatever you want to be the homepage message for your readers) and then over on the right hand side of the page, you should see something that says “Post Slug” with a plus sign next to it. Click that and type “homepage” in the text box (without the quotes). Now, we code…

Fire up your favorite text editor and open up either your home.php or index.php template. Assuming you know how to code HTML and CSS, I’ll not go into how to markup your page. However, once you’ve got a place marked up to place your homepage message and title, insert the following code:

<!–the loop–>
<?php query_posts(‘pagename=homepage’); //pull contents from homepage ?>
<?php if (have_posts()) : while (have_posts()) : the_post(); ?>
<h2 id="post-<?php the_ID(); ?>"><?php the_title(); ?></h2>
<?php the_content(""); ?>
<?php endwhile; endif; ?>

See Also
silver and gold round coins in box

What this will do is pull the title and content (including all styling elements). That means you’ll need to style the paragraph and header tags. Also, feel free to wrap the blurb above in a div to give you more control over the look.

Next week, I’ll go through how to add a section to you homepage that displays the title and excerpt from the latest entries in your blog. I post on Thursdays, so be sure to check back!

Neuroscientist reveals a new way to manifest more financial abundance

Breakthrough Columbia study confirms the brain region is 250 million years old, the size of a walnut and accessible inside your brain right now.

Learn More

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

8 subtle signs a man is deeply attracted to you but trying to play it cool

8 subtle signs a man is deeply attracted to you but trying to play it cool

Hack Spirit

If you want to break free from an unhappy and co-dependent relationship, say goodbye to these habits

If you want to break free from an unhappy and co-dependent relationship, say goodbye to these habits

Small Business Bonfire

6 zodiac signs who are most likely to be successful entrepreneurs

6 zodiac signs who are most likely to be successful entrepreneurs

Parent From Heart

8 tiny behaviors that speak volumes about a person’s intellect (or lack of it)

8 tiny behaviors that speak volumes about a person’s intellect (or lack of it)

Global English Editing

People who stay mentally sharp in their 70s and beyond usually adopt these 8 daily habits

People who stay mentally sharp in their 70s and beyond usually adopt these 8 daily habits

Bible Scripture

9 things highly respected people never reveal about themselves in public

9 things highly respected people never reveal about themselves in public

Hack Spirit