So while making some changes to my office’s public website, I ran into an issue in which i needed to display the latest three posts from the site. Because we are using a static home page, I can’t just go into Settings > Reading and set the “Blog pages show at most” to 3. I also could have edited my functions.php file to include a specific sidebar widget for the theme and add in a hook to call that sidebar widget on the home page’s custom page template, but why add more clutter to the back end when users might mistake the home page sidebar widget for the actual site sidebar widget when they’re making changes? And no one is ever really going to need to remove this functionality from the home page anyways, so it would be silly to add that as an option.
With a little Google magic, I stumbled upon this post by Perishable Press which details how you can use WordPress built-in function, wp_rss(), which negates the need for another plugin. On their site, they show you how you can do it in three ways: the easy way, the advanced way, and a way with SimplePie. I chose the advanced method and this is what I used, copied verbatim from Perishable Press’ post, that worked great for my needs:
include_once(ABSPATH.WPINC.’/rss.php’); // path to include script
$feed = fetch_rss(‘http://domain.tld/your-feed/’); // specify feed url
$items = array_slice($feed->items, 0, 7); // specify first and last item
<?php if (!empty($items)) : ?>
<?php foreach ($items as $item) : ?>
<h2><a href="<?php echo $item['link']; ?>"><?php echo $item['title']; ?></a></h2>
<p><?php echo $item['description']; ?></p>
<?php endforeach; ?>
<?php endif; ?>
I changed the $feed to match my website’s feed. I also changed the $items to 0, 3 because I only wanted the latest 3 posts. I removed the <?php echo $item['description']; ?> because I only wanted the titles to show. With that, I had the latest three posts coming into my front page which is a WordPress page designated as the home page and uses a custom page template. I definitely recommend visiting Perishable Press’s post to see all the different ways you can call a post using the wp_rss() function.
You can also try this solution by Corvid Works which is meant for non-WordPress pages.You can leave a response, or trackback from your own site.