Wordpress: Latest posts below your static front page

The latest posts functionality of Wordpress is quite nice, but most of the time I would like to use a static front page for customers. Unfortunately there is no default option to combine both types. It could be helpful for showing the latest posts, maybe from the news category, directly and exclusively at the front page. There are multiple ways to achieve this and I would like to introduce two of them:

Two ways to get it working



  1. If you know your theme very well, you can inject this feature into your theme. I don't want to elaborate this option too far, so just a few cues. You can use a special front-page.php or just an if statement in the pages template file asking if the current file is_front_page()

  2. The easier and more promising option is to use the the_content filter to add the latest posts below the content. To query the latest posts you should use wp_get_recent_posts. There is a small drawback in using this method. Typically the generated latest posts section will be included in the page-content container (e.g. <article>). That can have an impact on the SEO-rating of your page. But I wouldn't be too concerned about this because we are talking about the front page. Check out the example code below.


add_filter( 'the_content', 'devdev_show_latest_posts' );

/**
* Adds a latest posts section to the front page (static page)
*
* @author Hendrik Schuster <contact@deviantdev.com>
* @since 1.0
*/
function devdev_show_latest_posts( $content ) {
//check if the current page is the front page (static page)
if ( is_front_page() ) {

// add the heading for the section
$latestsPosts = "<h2>Recent Posts</h2>";

// start the latest posts list
$latestsPosts .= "<ul>";

// get the latest posts using your customized arguments
// http://codex.wordpress.org/Function_Reference/wp_get_recent_posts
$recent_posts = wp_get_recent_posts( array( 'numberposts' => 3 ) );

// render every single post to display as <li> element
foreach( $recent_posts as $recent ){
$latestsPosts .= '<li><a href="' . get_permalink($recent["ID"]) . '">' . $recent["post_title"].'</a> </li> ';
}

// close the latest posts list
$latestsPosts .= "</ul>";
}

//return the normal content and add your generated posts list
return $content . $latestsPosts;
}