How to use WordPress as a CMS part 3

In the past 2 posts I covered the menu and the home page.
And here is how to have a custom home page that isn’t static.A static page is not the best thing for a home page because it’s just kind of dull– a freshly updated and interesting home page is more exciting,better for SEO,better for site visitors,etc. It’s the home page, after all. It’s usually the 1st thing people see when they land on your website. Best to have new stuff there all the time.

I was working on a theme for an ecommerce site. My client had an idea to have a few sideboxes on her home page that showed her latest products. I had the idea that she could have a what’s new category, write posts to it and they’d show on the home page. We didn’t end up using it,though.

It was a bit of work to figure out so I wanted to pass it on.

Here is a custom query to show 3 excerpts from a single category in your template for the home page (and keep things fresh):

<?php $more = 0; ?> <?php query_posts('cat=2&showposts=3'); ?> <?php if (have_posts()) : ?> <?php while (have_posts()) : the_post(); ?> <div class="post" id="post-<?php the_ID(); ?>"> <h2 class="post_title"><?php the_title(); ?></h2> <div class="post_category">Posted in <?php the_category(', ') ?></div> <div class="post_comments"><?php comments_popup_link('No comment', '1 Comment', '% Comments'); ?> <?php edit_post_link('Edit', '', ''); ?></div> <div class="post_datetime"><?php the_time('F jS, Y') ?> at <?php the_time('g:ia'); ?></div> <?php if (is_search()) { the_excerpt(); } else { the_content(__('Read the rest of') . "\"" . the_title('', '', false) . "\" &raquo;"); } ?>

Ok, if you copy and paste that you’ll break your page because the divs aren’t closed and neither is the Loop.
I’ll try to explain what each bit does.


<?php $more = 0; ?>

That makes sure the more tag will be used in the Page, the more tag is just one of the template tags reserved for Posts… and Posts being what WP was made to make…


<?php query_posts('cat=2&showposts=3'); ?>

What that does is pull the latest 3 posts from category ID 2

some things may go awry:

Your client may forget which category is going to show on the home page.

That’s why it’s a really good idea to walk them through the dashboard and explain the whole category thing to them.

Also, name the category something like Home Page News.
So they will be more likely to recall this is the category for displaying post excerpts on the Home Page.

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

The good old Loop.

<?php if (is_search()) { the_excerpt(); } else { the_content(__('Read the rest of') . "\"" . the_title('', '', false) . "\" &raquo;"); } ?>

A more complicated bit of code to show content as an excerpt if it’s a search.
There are better ways to do it than this because my way relies on the person writing the posts(the client) remembering to put in the more link and choose the right category for the posts.

Here is the home_page.php file in full.
This template is only an example and if you can’t use it as is it’s because
you have to change the names of the divs to match the ones in your theme and make sure they’re all closed.That means checking the header and footer,too.
And the category ID,too,of course
-I forgot to mention that.

download template

Comments are closed.