Creating a “Blog Page” — With Paging

May 14th, 2008 Nathan Rice

If there was one question I get asked the most about WordPress themes, it’s this one: how do I make a custom static homepage at the root of my blog’s address (e.g. http://example.com/) and have my blog posts go into a /blog section (http://example.com/blog)?

WordPress is becoming more and more usable as a Content Management System, so naturally the question comes up.

But they don’t want to abandon the idea of a blog either. Blogs can be an integral part of a business website, and it makes sense that businesses and individuals would want the site and the blog managed from the same WordPress installation.

The First Option

The idea is simple … create a category called “blog” and place every single post you create in this category by making it the default category. Make all other categories “children” of the blog category.

This is actually a pretty decent option for users with a fair amount of WordPress experience. It allows you to accomplish the desired effect, and also lets you create other top-level categories for other purposes than categorizing posts (for instance, using top level categories for things like setting feature posts, press releases, or like me … theme releases).

The Second Option

The second option is a little more complicated, but is certainly a viable option for theme developers releasing themes for mass distribution (this is the option we use at iThemes. See the iCompany theme as an example.).

Here’s a step-by-step:

  1. Copy your index.php file
  2. Create a new file called blog.php and populate it with the contents of the index.php file
  3. Make it a page template by pasting the necessary code at the top. (making sure to change the template name to “Blog” instead of “Bio”)
  4. Create a new WordPress “page” (Write > Page) from your Dashboard
  5. Don’t worry about content on the page, but make the title “Blog”
  6. Before you click publish, scroll down to the “Advanced Options” and expand the “Page Template” section.  Choose “Blog” from the drop down menu.
  7. Click Publish.

Now, you’re going to want to edit the blog.php file one last time. Find “the loop“. It should look something like this:

< ?php if (have_posts()) : while (have_posts()) : the_post(); ?>

Replace that code with this:

< ?php
$temp = $wp_query;
$wp_query= null;
$wp_query = new WP_Query();
$wp_query->query('showposts=5'.'&paged='.$paged);
while ($wp_query->have_posts()) : $wp_query->the_post(); 
?>

You can change the “showposts=5″ to whatever number you want. It just depends on how many posts you want to be shown on a single page. Plus, but using ‘&paged=’.$paged we are able to keep paging intact … a problem that kept many people from using this method in the past.

Then, find the line of code that looks like this and remove it:

< ?php else : ?>

You’ll also probably want to remove any thing that has to do with not being able to find the page you were looking for. Hopefully you have a 404.php file that will take care of that for you.

Finally, after you’ve done the above, find the following code:

< ?php endif; ?>

and change it to:

< ?php $wp_query = null; $wp_query = $temp;?>

The Third Option

The final option is by far the easiest for the end user. It requires no code editing at all and accomplishes the same goal as the other 2 methods. The only problem is that it does screw up the “current_page_item” class for the home link and the blog link. Check out the possible solutions here.

Anyway, here’s how to do it.

The first thing you need to do is create a blog Page. Nothing fancy here, just write a new Page, title it “Blog”, and click “publish”. No need for any content.

Then, Assuming you’re still on the dashboard, click on Setting > Reading. Once there, you should see something that looks like this:

Default Reading Settings

Go ahead and click the radio button that says “A Static Page”. Then, you’re going to want to choose a page to use as your front page (any one of your pages will do), and you’re going to want to choose your “Blog Page” as the page to use for the posts.

Voila! You’re done! Save the settings and you should now be using one of your Pages as your homepage, and your blog Page is now housing all of your posts (and yes, paging does work with this method as well).

Conclusion

So there you go! Three different ways to accomplish the same general goal. If you have any suggestions for a better way of getting this done, feel free to leave a comment below and if it works well, then I’ll add it to the post.

The Tags: , , , , , ,

10 Responses to “Creating a “Blog Page” — With Paging”

Murk Said:

… or install WP in a subdirectory, and have a static page, or a page produced my other means, in the higher directory.

Comment made on May 15th, 2008 at 6:16 am
Nathan Rice Said:

@Murk,
The idea is to be able to manage your entire site from within the same installation of WordPress. By moving it to another directory means you have to have 2 separate installs.

Comment made on May 15th, 2008 at 11:15 pm
Sean Gaffney Said:

Hey Nathan,

I do something similar to Option 2, however the contents of my blog.php is simply this:

This is the method recommended in the WordPress codex, as shown here:
http://codex.wordpress.org/Making_Your_Blog_Appear_in_a_Non-Root_Folder

Hope that helps someone!

Comment made on May 16th, 2008 at 7:04 pm
Sean Gaffney Said:

I guess the code got stripped… probably for the best. However, it’s still available from the link listed.

Comment made on May 16th, 2008 at 7:05 pm
Nathan Rice Said:

@Sean Gaffney
Yeah, query_posts is great. But using WP_query helps avoid loop conflict. Either one is fine as long as you aren’t trying to run multiple loops.

Comment made on May 16th, 2008 at 7:14 pm
Chip Said:

I really like the first option, with the category “Blog”. I intend to change one of my blogs into a static web site, however I don’t want to lose the WP functionality, so I will create a “Blog” category, and everything will be listed there (there’s really not that many articles, that’s the reason).

Thanks for the tips :)

Comment made on June 11th, 2008 at 1:52 am
Team Nirvana Said:

This is surely helpful.

I was thinking of doing the same. So, left out my main page blank and only populated the /blog section with installation going on in it.

Would try this for sure.

Thanks for sharing.

Comment made on June 29th, 2008 at 9:46 am
JamesSpratt.org Said:

I go with @Murk’s suggestion. It’s what I do on my site and it works for me, I just hope it works for the average visitor too.

And be sure to use Lester Chan’s WP-PageNavi when it comes to the pagination itself.

Comment made on July 3rd, 2008 at 7:59 am
BeemiaNar Said:

tests time mashine

Comment made on July 8th, 2008 at 7:22 pm
bot Said:

cool site , thanks

Comment made on July 10th, 2008 at 5:41 pm

Trackbacks

 

Leave a Comment