User Tools

Site Tools


show_5_recent_blog_posts

Show 5 Recent Blog Posts on Static Front Page

To have a separate template for your front page, you need a front-page.php file. In that file, your normal wordpress loop will be in there.

Underneath the WordPress loop you can do a custom query using wp_query. In the WordPress Codex for WP_Query you can find the usage examples. Copy the code below and use CSS classes to style the look. The code below will display the 5 recent blog postings, the title will be an h4 headline that hyperlinks to the post itself. The excerpt is in paragraph form, with a special span class in case you need to change it's typography css.

<!-- recent posts -->
<h2>Recent Blog Posts</h2>
<?php
 
// The Query
$the_query = new WP_Query( array( 'posts_per_page' => 5 ) );
 
// The Loop
while ( $the_query->have_posts() ) : $the_query->the_post();
	echo '<a class="front-pg-blog-post-hdrs" title="Click to read this post" href="';
	the_permalink();
	echo '">';
	echo '<h4>';
	the_title();
	echo '</h4>';
	echo '</a>';
	echo '<span class="front-post-excerpt">';
	the_excerpt();
	echo '</span>';
endwhile;
 
// Reset Post Data
wp_reset_postdata();
 
?>
 
<!-- end recent posts -->

Alternative Bullet List

There's an alternative to just grab at bulleted list of the recent 5 posts from the archives. Use the code snippet below in your WordPress template.

<ul id="recentposts">
<?php get_archives('postbypost', '5', 'html', '', '', FALSE); ?>
</ul>

And of course you can use CSS to style the recentposts id or your own preferred class added to the UL tag.

show_5_recent_blog_posts.txt · Last modified: by 127.0.0.1

Donate Powered by PHP Valid HTML5 Valid CSS Driven by DokuWiki