28. January 2007 · Comments Off on Using a single post as a home page in WordPress · Categories: WordPress · Tags: , , , , ,

UPDATE:
WordPress 2.1 allows you to set an page (not post) as your front page easily. Everything below is still valid if you need a post instead, but given the new built in functionality, using a page is now a preferred option. See here for details.

UPDATE #2:
Ok, so don’t do this. It only works until you have a enough posts that it would have moved your one post off the front page. Since your page may be limited to, say, 10 posts, if you’ve written 10 posts after this the hack below makes WP think it’s posted those ten, so it stops before getting to the post you want. Obviously updating the time-stamp is a workaround, but a silly one.

So I’ve been looking at perhaps moving The Center for Effective Discipline’s website to use WordPress for the back-end. The site is huge, and needs to be moved to either a CMS system or WordPress. I’ve heard about speed and complexity issues with CMS systems, so right now we’re going down the WordPress avenue.

Anyhow, using WordPress 2.1 I wanted to use one post as the homepage. I could have easily just downloaded Semiologic’s Static Front Page Plugin, but then I would have had to use a page. I’m looking at using pages for navigation, so while that’s still an option, I went the post route instead. This requires a few changes.

I ended up basically just creating a post in its own category, and then limiting the homepage to just that category. These changes are probably hackier than you need, but for future reference and in case someone else is trying the same, here’s what I did:

  1. Create a category or use one that only this post will be in. You’ll need the ID for this category (you can see it in Site Admin -> Manage -> Categories). I used “Uncategorized”, which is ID 1, since all articles on this site going forward need to be in an existing category.
  2. Then you need to change the index.php file for whatever theme you’re using. Here is my file, with my additions in bold:

    < ?php get_header(); ?>
    <div id="content" class="narrowcolumn">
    < ?php if ($posts) { ?>
    < ?php foreach ($posts as $post) : start_wp(); ?>
    < ?php if ( (in_category('1')) || !is_home() ) { ?>
    < ?php require(TEMPLATEPATH. "/post.php");?>
    < ?php } ?>
    < ?php endforeach; ?>
    < ?php if(!is_home()) { ?>

    < ?php posts_nav_link(' — ', __('« Previous Page'), __('Next Page »')); ?>

    < ?php } ?>
    < ?php } else { ?>
    (... rest of file)

    This boils down to adding two “IF” statements. The first says “only display posts from category 1 if this is the home page.” The second says, “if we’re on the home page, don’t put navigation aides (like “next post”) even if WordPress thinks it needs them.” That second IF statement is needed because WordPress thinks it’s about to display all your most recent posts, only when it’s about to display them we told it to filter based on the first IF statement. By the time the code gets down to the navigation links, it doesn’t know that we filtered the posts and thinks it has displayed lots and lots. As such, if you’re using this hack to just display one category on the home page, don’t use the second IF statement!

  3. That’s it. Save and upload the new index.php file and you should be good to go.

Another post on how to create a hierarchical navigation system (that shows categories and all of the posts in the category you’re currently in) in the sidebar is coming soon. Try not to dance in the streets with excitement.

Comments closed.