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 Responses to Modifying WordPress’ Front Page

  1. Don’t take this as a knock at your efforts, but I believe there are other ways to do this without having to edit WP core files.

    The Subtle WP theme from http://www.gluedideas.com/ allows you to specify how many posts to appear on the index page and puts teasers at the bottom. It has other features and abilities that go beyond the capabilities of just about all other themes.

    I believe that WordPress also gives you the option (as of WP 2.2) to replace the main index page with a “Page” instead of the default index of the latest posts.

  2. Sorry Shaun, I think you missed the “index.php of your blog’s template” bit… this is completely independent of core WP files and based totally in the template.

    The purpose of this “how to” is to show people interested in creating their own themes (or modifying their current themes) to be able to display with a bit more customization. Using another pre-fab theme defeats the purpose.

    And you’re right, you can use a WordPress Page as your landing page in the latest version of WP. However, that is also outside the scope of this post and does little in the way of what I was looking to do. To add this sort of functionality to a WordPress page, you’d need to set up a custom page template, hardcoding the above PHP into it, and then use that as the Page set as a landing page. The result would be the same, but it’s a bit redundant.

  3. I read the post too fast. I thought you were suggesting that people modify the index.php at the root of their blog. The theme main index page unfortunately, has the same name “index.php”.

  4. For the sake of avoiding such confusion, I wonder why they didn’t just give the template files an arbitrary extension like .tpl or whatnot. Meh.

  5. Hey Ryan,

    I like that layout – I might ‘steal’ some of your ideas and modify my front page in a similar way. It does look better than long post after long post…

    Good advice and code.

  6. Cheers Jeremy. Look forward to seeing what you do with it. For myself, I’m a long way from being finished, but as this blog is a hobby, and the other things on my plate aren’t… a man has to have priorities ;-)

  7. Ryan,

    Just curious…..

    How do the aggregators feel about the abbreviated posts? Bloglines cut me off when I went to synopses, so I changed back….

    Things are looking great!

  8. I read synopsis feeds all the time through bloglines. Sure it wasn’t a glitch? I believe WP publishes the feed based on your backend settings, not on how they’re set to display on the front page.

  9. Pingback: Barking at the Sun || Blog

  10. Hi Ryan,

    Thanks for the great post. This is extremely helpful to me.

    Except for one problem: My posts are displaying in ascending order (i.e. no matter what I do, my first post is “stuck” in the “newest” post position).

    I copied your code almost exactly. I’m developing on WP 2.5.1. Not sure if there’s any other info you need, but any advice would be greatly appreciated.

    Thanks again!

  11. For some reason, I copied/pasted your code and my “Previous / Next” links below don’t show up. Maybe it’s a compatibility issue with 2.7?

  12. Hi Ryan,

    your post is very helpful for me and it’s almost wat i want for my website. I’ve created a static frontpage and a page for my blog. I’m goiing to make seperate template for both pages but for testing sake i only use the index.php for now.

    I have added the standard loop to be able to see the content of the page home and blog.

    Everything is working fine except for one important thing.
    The read on or read more link isn’t working on the homepage.
    On /blog it’s working fine.

    Is there a way to make it work, on both pages?

  13. @mlavdh: If you copied and pasted any of the code from this page – make sure you double check the single and double quote marks – that might be hanging things up.

    Be sure to check out the Codex: Customizing the Read More – it will show you how to add, remove or otherwise customize the “read more” link.

  14. Interesting post. Working on a modified front page post display as well. This might just help!

    P.S. Your code box’s text leaves the box in the latest Firefox version and goes to the end of the browser window..

  15. When I try the link I get . . .
    Read on »’); ?>
    Posted in
    Read on »’); ?>
    Posted in

    Not Found
    Sorry, but you are looking for something that isn’t here.

  16. Wait, sorry Keith – you’re not having troubles accessing the actual file, you’re having troubles getting it to work? Your first post seemed to indicate that the link wasn’t working.

    As for the file itself, make sure all the single- and double-quotes in the code are regular straight quotes, not fancy quotes. Also, I’m assuming you changed index.php.txt to index.php.

    If neither of those things work, I’m afraid I can’t help you. The code is 2 years old and hasn’t been tested with newer versions of WP. That said, having a quick look at it, it still looks like it should work, as the get_posts() function is still in use.

  17. Hi Ryan,
    Sorry to be a bother. If you have time could you mark this file (my working index.php)with where the variables and loops go? If no time no drama and I will continue to try different ways.

    Thanks,
    Keith

    <div class=”post” id=”post-“>

    <!– by –>

    <?php the_tags(‘Tags: ‘, ‘, ‘, ”); ?> Posted in | <?php comments_popup_link(‘What do you think?  »’, ‘1 Comment »’, ‘% Comments »’); ?>

    Not Found
    Sorry, but you are looking for something that isn’t here.

  18. Hi Keith, I really don’t recommend this solution for beginners, or for people just looking to mod the default theme. There are loads of free themes out there that do a “magazine” style front-page and many of them use solutions similar to this. My advice is to download one of those themes and see how they do it.

    This isn’t about adding things to the current loop, but rather recreating multiple loops to suit your purpose.

    As an update to this post: I should mention that the use of get_posts() is probably not the best method of doing this – but rather through the use of wp_query(). When I have time I’ll write an update about this. But until then, I definitely suggest searching Google and the WP codex.

  19. Need some help …

    I’m redesigning the homepage of FantasyCollegeBlitz.com and am trying to excerpt the latest blog entry.

    Test page in progress:
    http://www.fantasycollegeblitz.com/index-V3.php

    Error I’m getting:
    Fatal error: Call to undefined function have_posts() in on line 1

    You can see from this other page that I had excerpted only the headlines with no issues: http://www.fantasycollegeblitz.com/index-V2.php

    So my attempt to display an excerpt of the latest entry is failing. I modified your code to use only “the newest” and remove references to “the newer” and “the new”.

    *** HERE IS ALL I’M USING ***

    <div class=”post” id=”post-“>

    <!– by –>

    <?php the_tags(‘Tags: ‘, ‘, ‘, ”); ?> Posted in | <?php comments_popup_link(‘What do you think?  »’, ‘1 Comment »’, ‘% Comments »’); ?>

    **** end coding ****

    Thoughts?

  20. whoops that’s not all the coding i’m using … can’t seem to paste it correctly … but I am omitting the “newer” and “new” refrences as i mentioned

  21. Unfortunately code wont display properly in comments. :-(

    It’s difficult to tell where you might be going wrong here Patrick, but that you are running php files outside of the WordPress theming structure feels like it might be the cause of it. Because these functions (ie. have_posts()) are internal WordPress functions, you need to make sure you test your code in the confines of a theme – and not independently from it.

  22. Ryan:

    Yep that’s exactly what I’m doing — attempting to create a homepage separate from the WordPress templating system that serves like a portal to the site’s content as well as feature other content from the site’s social media efforts. While I am able to easily incorporate php require commands to various files in an includes directory I setup, I thought it would be easier to setup this sort of separate homepage while still utilizing some WordPress coding. I did a site once that ran on ExpressionEngine and was able to do whatever I wanted to … seems like working with WP is not as simple as I had hoped.

    So it sounds like what I want to do cannot be done unless I create a homepage template within the WP confines?

    Thanks for taking a look.

  23. @Patrick: I think you’d be much better off tackling it within the scope of WordPress – which is easy enough to do once you are comfortable with its rather robust templating system.

    To have a completely custom front page, while still in the confines of WP, I would create a page in WordPress’ back end and assign it a custom page template which would include all the PHP (internal to WP and otherwise) that I want to display. Then set the default homepage in the WP settings to that page.

  24. Ryan:

    OK will give that a shot .. my initial dive into WP’s admin panel left me wary of what and how I could manipulate & customize … will follow your suggestions and report back ;-)

    Thanks again fer all the hep.

  25. Your biggest changes will be in your theme’s folder:
    /wp-content/themes/your-theme-name/*.php

    The default WP theme comes with a sample template file called “archives.php” – this will give you an idea of how to set up a Page Template – but the WP codex is a good place to go as well.

    Good luck!

  26. It is extremely annoying trying to figure out what you have going on there when your code spills out over your sidebar links and whatever else.

  27. This is the best. I’ve spent hours trying to figure this out on my own. Not succeeding I turned to Google and landed here.

    Thanks champ.

  28. Just a thought… why do you need to loop the first one when there is nothing to loop?

    And another thought why would WP set up their templates with .php and clutter the template files with ” tags. wouldn’t it be better to set it up with .tpl extensions and some custom tags for the functions …

    Hmmm just a thought…don’t go ballistic !!

  29. @Stone Deft: You need to understand what The Loop is in how it relates to WordPress to answer that question. It has to “loop” to call the posts. Though it’s likely possible to load the variables without the need for a while loop, it’s just a simple structure to follow.

    Ditto the PHP “tags”. A .tpl extension is just an extension, as are the .php extensions of the WP template files. They’re not actually being run independently from the overall construction of the page. Just rather than creating a proprietary templating language that the developer would need to learn, they use PHP functions.

    For anyone that is familiar with PHP, it’s not cluttered at all. And for anyone that isn’t, even if it was a proprietary theming system (or an open source one like Smarty) it would still be “cluttered” to someone unfamiliar with it. At least it adheres to a standard of knowledge that is transferable to a number of other CMS-based system (ie. Drupal or Joomla, both of which are PHP-based — though Joomla tends to use a bit more of a proprietary system of theming).

    Who’s going “ballistic !!” ?

  30. @Ryan of course you can do it without the loop here’s how:
    $t = get_posts(“numberposts=1″);
    setup_postdata($t);
    the_title();
    //post the contents here etc etc etc…

    I still think it’s cluttered. I’ve been developing my own custom built CMS for my clients and as a gorgeous web developer I always separate php from html, separate presentation from logic. That way when a client wants to edit his templates he won’t freak out with the clutter of php codes. Hey vbulletin and phpbb3 is doing it. I always use bTemplate and yes I’m bent in integrating it on my first WP project. Still figuring out hooks though but Ill get there soon.

    Anyway WP is a good framework my kudos to all the developers. And please don’t go too ballistic.

    you look Great!

  31. @Stone Deft: Ah! Right. Sorry, was caught up in the concept, I didn’t even think about the fact that there’s no reason to loop through a single item.

    For my development needs, I find that sep. style from code is enough – it’s not to say that logic from structure (as presentation would really be style, no?) isn’t a fine thing to do, just a step too much for what I’ve found useful. I just try to make themes as flexible as possible, so my clients wont need to touch the code.

    As for the get_posts function — if you’re using multiple loops, you might want to look at generating multiple WP_query objects. I think it’s cleaner — and the way I do it now (this post is 2 years old).

  32. @Ed: It sure is. You need to customize the loop a bit though. Simply use any custom way to call the post (get_posts, wp_query, etc.) and use the ‘orderby=rand’ parameter. See more here.

  33. Fantastic coding.

    Is there a way this can be adapted to work as a category.php file? So essentially if I click a category link it feeds this layout for the category that is clicked?

    Any ideas?

    Thank you,
    Karl

  34. Hello Ryan, fab tutorial. It works perfectly for me – EXCEPT tags aren’t showing up now… even though I have called in the function… any ideas? Is this a known issue?

  35. The three snippets only differ on how the posts are displayed but not with respect time? You have three vars, the newest, newer, and new, but they show the same first post. It should be from the latest to the oldest which means the 2nd snippet should start with 2nd latest post and the 3rd should pick up where the 2nd ended. woot. This is useless. Thanks anyway.

  36. Very, very useful post. I used this code to do a “magazine” type home page where the newest post was a featured article, the next four were shown with thumbnails and the rest were just listed. Here it is: http://www.fcdynamos.com
    I have a problem now where the second page of posts is showing up the same as the first page. I’m going to ask at the WP forum … but if anyone has any ideas…

  37. Thanks for the article! And as I do now website.com/page/2/ because they do not work (shows the same thing)?

  38. My point here is that when I click on ‘more’ (site.com/page/2/), I see it the same as the homepage (the posts).
    Why is this happening?

  39. I was on the localhost. I can just see the rest of the posts. On the home page I have three loops, thus poradnikowi done. When you click on a link added next_posts_link() I see the same posts at the homepage.

  40. Hi Pablo,

    You’re going to want to add ‘paged’=>$paged variable to your query string. I’ve not tested the method outlined in the OP for a long time (this post is 3 years old — and I’ll definitely need to update it soon). But basically, try adding something like:

    $paged = (get_query_var(‘paged’)) ? get_query_var(‘paged’) : 1;
    $the_newest = get_posts(‘numberposts=1&paged=’.$paged);

  41. global $post;
    $the_newer = get_posts(‘numberposts=4&offset=1′);
    $the_new = get_posts(‘numberposts=6&offset=5&order=DESC&orderby=post_date’);
    $paged = (get_query_var(‘paged’)) ? get_query_var(‘paged’) : 1;
    $the_newest = get_posts(‘numberposts=1&paged=’.$paged);

  42. It also does not work:

    global $post;
    $paged = (get_query_var(‘paged’)) ? get_query_var(‘paged’) : 1;
    $the_newest = get_posts(‘numberposts=1&paged=’.$paged);
    $the_newer = get_posts(‘numberposts=4&offset=1&paged=’.$paged);
    $the_new = get_posts(‘numberposts=6&offset=5&order=DESC&orderby=post_date&paged=’.$paged);

    And this:

    global $post;
    $paged = (get_query_var(‘paged’)) ? get_query_var(‘paged’) : 1;
    $the_newest = get_posts(‘numberposts=1&paged=’.$paged);
    $the_newer = get_posts(‘numberposts=4&offset=1′);
    $the_new = get_posts(‘numberposts=6&offset=5&order=DESC&orderby=post_date’);

    • Make sure your single quotes are actual single quotes, and not fancy quotes — if you copied the code from my comment, it may have copied the fancy quotes as well, as WordPress auto-formats the comments.

      • Hi Ryan, I know this is an old post, but man does it ever look good on my site! Thanks. I’m having a similar issue to Pablo, i.e. page shows the same posts as page one. I tried the code you supplied but all it does is “bump” the posts down by one in each section. What I’d REALLY like to do is revert to a normal category loop on the 2,3,4… pages (thumb, title, excerpt) starting with the 10th newest post. Is this possible? website is http://foododdity.com. Thanks!

  43. Make sure you copy it into plain text format before pasting it in your scripts. Or the formatting could mess the code up.

  44. I found this helpful it works but there is one promblem, when you click on the navigation for previous post, the next page has the same post as the home page, and all other pages are the same, you are not able to navigate through the website with this code

  45. Hi,

    Thank you very much for your article.
    Can you give me some pointers on how to modify this code so that I get 2 loops repeated in groups of 6 through all my posts?
    I’d like to achieve this:

    <?php
    global $post;
    $the_newest = get_posts('numberposts=6');
    $the_newer = get_posts('numberposts=6&offset=6');
    $the_new = get_posts('numberposts=6&offset=12');

    But without having to copy-paste the 2 foreach loops all those time, instead making them simply count 6 posts and repeat themselves.

    Thnx!

  46. Én vagyok nyűgözve, azt kell mondani. Tényleg nem gyakran találkozom a blog, ami minden nevelő és szórakoztató, és hadd tájékoztatja Önt, hogy megvan találta a szöget a fején. A koncepció kiemelkedő, a nehézség az egy dolog, hogy nem elegendő az emberek beszélnek intelligensen kb. Én nagyon boldog, hogy belebotlottam ez alatt az én keresnek valamit ezzel.

  47. Hi. Thanks so much for giving out your code – I have been trying to do this for ages. I have a problem though, and not sure how to correct it … If I leave the code as is and in my wordpress settings – reading, I put 9 in my ‘Blog pages show at most’ then on the homepage I get 9 loops of all 9 recent posts. ( If I leave the above at 1 it all works fine on the homepage but then only 1 post appears at any time in an archive or category search which is not what I want). Can you advise?
    Thanks so much again,
    Jane

Leave a Reply

Your email address will not be published. Required fields are marked *

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=""> <strike> <strong>