WordPress: Customize That Homepage
Tagged as: WordPress
Until now, Space for a Name’s homepage was just a collection of all my posts. While you could read all my posts without leaving that one page, it wasn’t very attractive, and didn’t have any focus, except that granted by the ordering of elements on the page. What I really wanted was to put focus on the newest article while still allowing readers to see other recent posts and get an idea of what each post covers. Luckily, this only required a few small changes to my index.php.
Out With the Old, In With the New
If you are building your own WordPress theme, or are using a simple pre-built theme, your index.php will utilize The Loop, WordPress’s method of iterating through your posts. By default, it should look something like this:
<?php if ( have_posts() ) : while ( have_posts() ) : the_post(); ?>
...output your post data...
<?php endwhile; else: ?>
<p><?php _e('Sorry, no posts matched your criteria.'); ?></p>
<?php endif; ?>
A few things are occuring here. First, have_posts() is executed to see if there are any posts to display. If there are, a while loop is entered which continues running as long as have_posts() returns true. For each post, the_post() is executed, which populates various WordPress functions such as the_content(), the_tags() and the_permalink(). If there are no posts to display, a message is written indicating the fact. If we want to differentiate how we handle the latest post and posts after that, we need to create two loops. Luckily, the WordPress function query_posts() makes this feat easy.
The query_posts() function allows us to refine what posts are pulled into the have_posts() function. The function accepts a list of parameters to filter the posts you want to see. While there are many options available to you, we only need two: showposts and offset. To get only the most recent post, we need to add an additional line of PHP just before the start of our loop:
<?php query_posts('showposts=1'); ?>
Easy, right? Now we need to get our other recent posts, but showposts alone won’t do the job because each call to query_posts() starts from the first post. We need to use another parameter, like so:
<?php query_posts('showposts=10&offset=1'); ?>
With these two functions, your code should now resemble:
<?php query_posts('showposts=1'); ?>
<?php if(have_posts()) : while (have_posts()) : the_post(); ?>
...do something with your latest post...
<?php endwhile; else
>
...display error message
<?php endif; ?>
<?php query_posts('showposts=10&offset=1'); ?>
<?php while (have_posts()) : the_post(); ?>
...do something with your recent posts...
<?php endwhile; ?>
You’ll notice I don’t use if (have_posts()) in the second loop. If there isn’t a first post, an error message will be displayed, so there’s no reason to display another one when there isn’t a second post either. Likewise, if there is only one post, we don’t want to display a post and then display an error saying there aren’t any posts.
So now that I’ve got two separate loops set up, how best to display the post(s) in each loop? I decided that for the latest post, it was best to display the first paragraph or two using the <!--more--> HTML comment utilized by WordPress. Inserting this comment into your post indicates to WordPress that only the content before it should be displayed on the homepage with a link to ‘read more’. All content after this comment is only available by viewing the specific post page.
For my other recent posts, I decided that it was enough to simply provide a brief overview of the post’s contents, and therefore chose to utilize the ‘excerpt’ feature of WordPress posts. To display a post’s excerpt instead of the content itself, we swap out the_content() for the_excerpt(). Easy peasy.
Next Time, On Space for a Name
For returning readers (or anyone who’s visited Space for a Name’s evolution gallery, you’ll notice I’ve made a number of changes other than just editing my WordPress homepage. I’ve added a navigation menu, created a logotype, and added a background. I feel these changes constitute their own topic, so I’ll leave them for another day.
spaceforaname
com
Cheese puffs
If you’re angling to become a contributor, that comment doesn’t help your position…
Good point. I’ll start gradually increasing the relevance of my posts
[...] WordPress Customize That Homepage Space for a Name Posted by root 2 hours 13 minutes ago (http://spaceforaname.com) So now that i 39 ve got two separate loops set up how best to display the post s in each loop inserting this comment into your post indicates to wordpress that only the content space for a name is proudly powered by wordpress org Discuss | Bury | News | WordPress Customize That Homepage Space for a Name [...]