Adding Post Excerpts to your WordPress Static Front Page

  • 0
150 150 Adrian

I would have thought that solutions to this scenario would have been all over Google, but alas I had to dig to find snippets of information that eventually allowed me to write the code that I wanted.

What I wanted: A list of my top 5 blog posts to appear on my WordPress site’s static homepage, with the blog’s title and a small amount of the blog’s text. I also wanted the “more” text to be a link to the actual blog post.

Solution:

1. Add the following to your theme’s functions.php file (inside the <?php … ?> tags):

function home_excerpt_length($length) {
return 10; // The length of the excerpt text in characters
}

function home_excerpt_more($more) {
global $post;
return ' <a href="'. get_permalink($post->ID) . '">[more...]</a>';
}

2. Add the following to your front-page.php file (or whatever template file you are using) at the spot on the page where you want the blog excerpts to appear:

<?php
query_posts('posts_per_page=5');
add_filter('excerpt_length', 'home_excerpt_length');
add_filter('excerpt_more', 'home_excerpt_more');
if (have_posts()) :
while (have_posts()) :
the_post();
echo "<h3>";
the_title();
echo "</h3>";
the_excerpt();
endwhile;
endif;
?>
  • 0
Author

Adrian

Adrian, or AJ, is the founder and Director of Technology of Pop Digital. He has spoken at tech conferences around the world, and published numerous articles about Agile methodologies, UX design, Information Architecture, and Web Development.

All stories by: Adrian