Quite often recently when building WordPress themes for smaller sites (ones that have a brochure feel, but have a blog section), the design has included an area on the frontpage, or footer that shows a list of the latest blog posts.

I’ve got a little loop that I use for this area, shown below:

< ?php
global $post;
$tmp_post = $post;
$myposts = get_posts('numberposts=3');
foreach($myposts as $post) :
	setup_postdata($post);
	?>
	
  • < ?php the_date('d F Y');?>
    < ?php the_title(); ?>
    < ?php echo htmlentities(cut_str_length(strip_tags(get_the_content()), 80)); ?>
  • < ?php endforeach; $post = $tmp_post; ?>

    Apart from the cut_str_length function – which I haven’t included – this makes use of WordPress functions and in-built PHP functions to strip the text out of the content so that I have don’t have any images in the way or any formatting, so I can apply my own.

    Since the setup_postdata() function seems to have a “special relationship” with $post, we grab a copy before and restore after the function so that you can use it anywhere in the page – inside, or outside the loop – and it shouldn’t affect the rest of your page.

    Image Credit: Jasmic