Modifying WordPress' Front Page

The origins of the standard blog front page vary. Some say that the post-after-post-after-post style comes from the ancient Greeks, others say it was the Chinese. One thing’s for certain, it’s old and it’s tired.

Having been a blogger in one shape or another for going on five years now, I’ve started to become disenchanted with the main index layout common to near all blogging platforms. You know the one, a landing page that lists the 5-10 most recent posts with a page length about the size of Kazakhstan.

As such, I began hunting around for a way to modify it a bit more to suit my likings, and what I found was that it’s not hard at all.

The Goal

What I wanted to do was create a page that degrades the size of the post being shown based on its datedness. The most recent post would be showcased, with the three previous posts offering a teaser or excerpt. I then wished to cap things off with a short list of five links to the most recent entries after those first four.

The Method

To do this you need to break into the index.php file of your blog’s template. Generally speaking this file uses what WordPress calls The Loop (notice the caps, pretentious bastards).

Usually, The Loop looks like this:

[codesyntax lang=”php”]

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

<div class=”post” id=”post-<?php the_ID(); ?>”>

<h2><a href=”<?php the_permalink() ?>” rel=”bookmark” title=”Permanent Link to <?php the_title(); ?>”><?php the_title(); ?></a></h2>

<small><?php the_time(‘F jS, Y’) ?></small>

<div class=”entry”>

<?php the_content(‘Read the rest of this entry &raquo;’); ?>

</div>

<p class=”postmetadata”>Posted in <?php the_category(‘, ‘) ?> | <?php edit_post_link(‘Edit’,”, ‘ | ‘); ?> <?php comments_popup_link(‘No Comments &#187;’, ‘1 Comment &#187;’, ‘% Comments &#187;’); ?></p>

</div>

<?php endwhile; ?>

[/codesyntax]

Well to do what I wanted, I needed a slightly more customizable solution. Fortunately, the brainiacs that those WP developers are, they created an awesome little built-in function called get_posts(). Smartly named too, I might add.

This fun little guy creates mini-“The Loop“s, and allows you to specific things like the number of posts, what to sort by (name, date, etc.), whether you wish to sort ascending or descending, and what offset you want (i.e. how many posts after the most recent one do you want to start at).

So, to do what I need, I required three mini-“The Loop“s.

  1. The first would display just the most recent post with full text.
  2. The second would display three posts displaying an excerpt (teaser) of text.
  3. The third would list five more links displaying only the title and date.

The first thing we add to our code are the variables:

[codesyntax lang=”php”]

<?php

global $post;

$the_newest = get_posts(‘numberposts=1’);

$the_newer = get_posts(‘numberposts=3&offset=1’);

$the_new = get_posts(‘numberposts=5&offset=4&order=DESC&orderby=post_date’); ?>

[/codesyntax]

Immediately following this we set up our three loops. I know PHP can be overwhelming, particularly for those with little experience with it. However, by looking through the following code, you should see the similarities and differences that change the output to my preferences stated above.

The First Loop – The Most Recent Post:

[codesyntax lang=”php”]

<?php foreach($the_newest as $post) :

setup_postdata($post); ?>

<div class=”post” id=”post-<?php the_ID(); ?>”>

<h2 class=”posttitle”><a href=”<?php the_permalink() ?>” rel=”bookmark” title=”Permanent Link to <?php the_title(); ?>”><?php the_title(); ?></a></h2>

<div class=”entry”>

<?php the_content(‘<span>Read on &raquo;</span>’); ?>

</div>

<div class=”postmeta”>Posted in <?php the_category(‘, ‘) ?> | <?php edit_post_link(‘Edit’, ”, ‘ | ‘); ?> <?php comments_popup_link(‘No Comments’, ‘Comment (1)’, ‘Comments (%)’); ?></div>

</div>

<?php endforeach; ?>

[/codesyntax]

The Second Loop – Three Posts Summarized:

[codesyntax lang=”php”]

<?php foreach($the_newer as $post) :

setup_postdata($post); ?>

<div class=”post” id=”post-<?php the_ID(); ?>”>

<h2 class=”posttitle”><a href=”<?php the_permalink() ?>” rel=”bookmark” title=”Permanent Link to <?php the_title(); ?>”><?php the_title(); ?></a></h2>

<div class=”entry”>

<?php the_excerpt(‘<span>Read on &raquo;</span>’); ?>

</div>

<div class=”postmeta”>Posted in <?php the_category(‘, ‘) ?> | <?php edit_post_link(‘Edit’, ”, ‘ | ‘); ?> <?php comments_popup_link(‘No Comments’, ‘Comment (1)’, ‘Comments (%)’); ?></div>

</div>

<?php endforeach; ?>

[/codesyntax]

**Aside from the variable ($the_newest/$the_newer), the only thing that changes between these two loops is the bit on Line 6 (the_content(‘Read on’); and the_excerpt(‘Read on’);). This will change the display from the full content of the post to the alternate excerpt you can include with each post (if you don’t set an excerpt, WordPress simply defaults to the first 55 words of the post).

The Third Loop – Five More Links

[codesyntax lang=”php”]

<?php foreach($the_new as $post) :

setup_postdata($post); ?>

<strong><?php the_time(‘F jS, Y’) ?></strong><br />

<a href=”<?php the_permalink() ?>” rel=”bookmark” title=”Permanent Link to <?php the_title(); ?>”><?php the_title(); ?></a>

<br /><br />

<?php endforeach; ?>

[/codesyntax]

** Here we don’t have any the_content or the_excerpt, but instead just make a linkable title.

Wrap up

And there you have it, an alternative way to display your index.php file for WordPress blogs. Check out the front page of this blog to see it in action. And if you just want to cruise through the php of the index file, check it out (in text format) here:

index.php.txt

This mod opens up a lot more potential for methods in which you can display your blog’s content. Read through the get_posts() entry on WordPress’ Codex and figure out additional creative ways of modifying your home page.

86 thoughts on Modifying WordPress' Front Page

  1. Pingback: Barking at the Sun || Blog

Say something...

You may use these HTML tags and attributes: <a href="" title=""> <abbr title=""> <acronym title=""> <b> <blockquote cite=""> <cite> <code> <del datetime=""> <em> <i> <q cite=""> <s> <strike> <strong>