How to display WordPress posts anywhere in your theme

When looking to customise your WordPress theme, you may want to display posts from a particular category someone within a template.

Simply insert the below PHP where you want the posts to appear in your  template.

 global $post;
 $theposts = get_posts('numberposts=5&category=1');
 foreach($theposts as $post) :
 <?php the_title(); ?>
 <?php the_content(); ?>
 <?php endforeach; ?>

To Include a thumbnail within your posts simply ensure that your theme supports post thumbnails, then add

<?php the_post_thumbnail(); ?>

If your theme doesn’t support thumbnails then just add the theme support function to your functions.php file.

add_theme_support( 'post-thumbnails' );
Share this:

Leave a comment