On my photography site, I display a list of the last 5 blog posts in the footer on all pages. I use the rss feed to get the most recent posts outside of the wordpress environment.
This is how I do it using phplugins:
function dlp_get_latest_blog_posts( $limit, $link ){
$result = '<ul>';
$rss = new DOMDocument();
$rss->load($link);
$feed = array();
foreach ($rss->getElementsByTagName('item') as $node) {
$item = array (
'title' => $node->getElementsByTagName('title')->item(0)->nodeValue,
'link' => $node->getElementsByTagName('link')->item(0)->nodeValue,
);
array_push($feed, $item);
}
for($x=0;$x<$limit;$x++) {
$title = str_replace(' & ', ' & ', $feed[$x]['title']);
$link = $feed[$x]['link'];
$result .= '<li><a href="'.$link.'" title="'.$title.'">'.$title.'</a></li>';
}
$result .= '</ul>';
return $result;
}
And then I can call $this->dlp_get_latest_blog_posts(10, ‘my-link’) wherever I would like to show a list of the 10 most recent blog posts. For this lab site, I use
function pallet02_top() {
if ($this->dlp_page_match('/blog')) {
// don't show custom right sidebar on blog pages
return true;
} else {
// Recent posts
echo '
<div id="recent-posts-3" class="widget widget_recent_entries">
<h2 class="widget-title">Recent Posts</h2>
'.$this->dlp_get_latest_blog_posts(5, "https://lab.danielleu.com/blog/feed/").'
</div>
';
}
}
I only show my custom sidebar on my non-blog pages. That’s why I filter them using my page match function dlp_page_match:
function dlp_page_match($page) {
if (substr($_SERVER["REQUEST_URI"], 0, strlen($page)) == $page) {
return 1;
} else {
return 0;
}
}
Please note that dlp_page_match must be placed before the pallet02_top function.
If you like to see only blog posts matching a category, just set the url as “your-domain.com/blog/category/category-name/feed/”.
Did you like this post? Did you use the given code? Please consider supporting me by buying me a coffee!
Thanks!