Quantcast
Channel: WordPress Tips Archives - Nathan Rice
Viewing all articles
Browse latest Browse all 15

Creating a “Blog Page”— With Paging

$
0
0

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:


Replace that code with this:

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:


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:


and change it to:


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.


Viewing all articles
Browse latest Browse all 15

Trending Articles