Here’s a nice simple snippet for you. Integrating your latest news posts into your website’s homepage is a great way to encourage engagement with your blog/news section. You can do some fancy stuff like bring in the featured image and overlay your post title over the top (we’ll cover this in more detail later) or simply show a list of blog posts with a bit of meta information (dates etc) and a summary of the article’s text. To get you started, this is a very basic snippet of code that will allow you to bring in the four latest news posts and display them anywhere you like on your homepage:
<?php query_posts('showposts=4'); ?> <ul> <?php while (have_posts()) : the_post(); ?> <li><?php the_date(); ?></li> <li><a href="<?php the_permalink() ?>"><?php the_title(); ?></a></li> <li><?php the_excerpt(); ?></li> </ul> <?php endwhile;?>
The ‘showposts=’ parameter at the top can be changed to whatever value you like and this will be the number of posts displayed. By default, the above code will display an ‘excerpt’ of text from your blog posts underneath each title – this is pulled in by the ‘<li><?php the_excerpt(); ?></li>’ line. If you don’t want to use an excerpt simply delete this line. Likewise, if you want to remove the date, just delete the ‘<li><?php the_date(); ?></li>’ line.