In last week’s post we explored the options for displaying recent posts from HostingCaddie, ThemingWP, and this blog on wilwebs.org.
With the proper plugin, displaying post excerpts – or even the full post content – is a piece of cake. What I prefer though, is a simple list resembling WordPress’ default recent posts widget, but with items from the three named blogs.
And it would be great when the list of recent posts on a specific page relates to the main content on that page. This way, the page regarding ThemingWP would display a list with recent posts from ThemingWP.com, the WPfy.me page a list with WPfy.me posts, and the HostingCaddie page a list regarding Hostingcaddie.com.
Can we create such a recent posts list with RSS feed items? Let’s see.
The plugin
HungryFEED and WP RSS Aggregator are both capable of producing a list with recent feed items. HungryFEED has not been updated for a while, so that is not the best solution with regard to security.
Which leaves us with WP RSS Aggregator. This plugin lets you produce lists of post titles with shortcodes. However, processing shortcodes in widget areas is not supported. To display a list with feed items in the sidebar, you need to purchase the Widget extension. Which I did.
With the Widget extension it is indeed possible to display a recent posts lists from feed items. The add-on generates a list with a number of recent posts from all feed sources. Unfortunately, it is not possible to display feed items from only certain feed sources. At least not yet.
According to Mark Zahra, Head Support Engineer at WP RSS Aggregator, a feature to select feed resources will be included in one of the upcoming updates for the Widget add-on. That is good news. But patience is not my biggest virtue.
To display only recent post items related to the main content with the Widget extension today, I would have to create a couple of widget areas. In combination with conditionals tags, it would be possible to display only the widget with recent post items related to the main content. Effective, but not very elegant.
The solution
When you check the installation page of the WP RSS Aggregator plugin, you see this function call:
<?php
wprss_display_feed_items( $args = array(
'links_before' => '<ul>',
'links_after' => '</ul>',
'link_before' => '<li>',
'link_after' => '</li>',
'limit' => '8',
'source' => '5,9'
));
?>
How about using this template, extending the code a bit and displaying it in the sidebar with the PHP Code Widget?
This is the extended code:
<?php
if ( is_page('hostingcaddie') ) {
echo'<h3>Posts from HostingCaddie</h3>';
wprss_display_feed_items( $args = array(
'links_before' => '<ul>',
'links_after' => '</ul>',
'link_before' => '<li>',
'link_after' => '</li>',
'limit' => '5',
'source' => '254'
));
}
elseif ( is_page('themingwp') ) {
echo'<h3>Posts from ThemingWP</h3>';;
wprss_display_feed_items( $args = array(
'links_before' => '<ul>',
'links_after' => '</ul>',
'link_before' => '<li>',
'link_after' => '</li>',
'limit' => '5',
'source' => '260'
));
}
elseif ( is_page('wpfyme') ) {
echo'<h3>Posts from WPfy.me</h3>';
wprss_display_feed_items( $args = array(
'links_before' => '<ul>',
'links_after' => '</ul>',
'link_before' => '<li>',
'link_after' => '</li>',
'limit' => '5',
'source' => '266'
));
}
else {
echo'<h3>Posts from the blogs</h3>';
wprss_display_feed_items( $args = array(
'links_before' => '<ul>',
'links_after' => '</ul>',
'link_before' => '<li>',
'link_after' => '</li>',
'limit' => '5',
));
};
?>
What we see are four recent posts templates. One for each of the blogs (HostingCaddie, ThemingWP, and wilwebs.com), and another one for the remaining pages on wilwebs.org, like for example the privacy policy.
The recent posts are pulled from the feed sources (254, 260, 266), and displayed on the blog pages (determined by the slug of the page). Except for the last template, which does not specify a feed source, and therefore display posts from every feed sources.
The code works, but is not very efficient. There is a lot of repetitive code, which is not good practice. Basically there are three variables; page slug, widget title, and feed source. Let’s simplify the code a bit.
The following code takes only about half the number of lines, but produces the same outcome:
<?php
if ( is_page('hostingcaddie') ) {
$recent_posts_title = '<h3>Posts from HostingCaddie</h3>';
$recent_posts_source = '254';
}
elseif ( is_page('themingwp') ) {
$recent_posts_title = '<h3>Posts from ThemingWP</h3>';
$recent_posts_source = '260';
}
elseif ( is_page('wpfyme') ) {
$recent_posts_title = '<h3>Posts from WPfy.me</h3>';
$recent_posts_source = '266';
}
else {
$recent_posts_title = '<h3>Posts from the blogs</h3>';
$recent_posts_source = '';
}
echo $recent_posts_title;
wprss_display_feed_items( $args = array(
'source' => $recent_posts_source,
'links_before' => '<ul>',
'links_after' => '</ul>',
'link_before' => '<li>',
'link_after' => '</li>',
'limit' => '5',
));
?>
The verdict
Yep, we did it! Every inner page counts the same number of sidebar widgets – four. And the contents of the fourth widget relates to the main content of the web page.
Not convinced yet? Fly over to wilwebs.org and check for yourself .
Recent comments