Integrating a WordPress Blogroll into a MODx Sidebar
<?php
include_once("rss/rsslib.php");
$output = '<div class="call-feed blog">'.
' <h4>Recent Blog Posts</h4>';
$output .= RSS_Display("http://www.example.com/rss", 6);
$output .= ' <a class="view-all" href="/blog/">'.
'View All Blog Posts</a></div>';
return $output;
?>
Now, a bit of an explanation of the code above. The first line is essential. If you do not include the RSS PHP code, your function call will not work. Make sure (again) that you are pointing to the right file! I surrounded the called code with a DIV to help with styling, but feel free to skip this step if you so wish. Same with the title. You will notice that I use the convention of concatenating the output to a variable to an appropriately named variable, '$output'. I do this because a MODx snippet expects a string to be returned at the end. As opposed to using echo() calls all over the place, this will allow for you to do pre- and post-formatting of the output. Always a nice thing. I added a link at the bottom to the location of the actual blog (a nice convention) as well as a DIV, again for styling.
I called [[blog-rss-feed]] from within the website template, but you can choose whichever template you would like. For instance, if you only want it on the front page, and have a template for the front page, throw it in there. Of note, I surrounded the snippet call with non-displayed codes so I could troubleshoot the output. Your call.
This effectively gave me a blog roll for the website! From another website!
Advanced Modifications:
Props go out to the peeps at ScripTol.com, but their output was a little less than semantic for our purposes. They surrounded each listing with an unordered list tag, UL, and each part of the listing with a list item, LI, tag. They separate some of the sections with line breaks, BR, and use the bold tag, B, for the first listing. Because we were needing a full semantic treatment, I cut these out and surrounded each listing with its own DIV class, and each title and description with its own. There was a "more info" convention we were using, so I put the "more info" link within its own DIV class as well.
What we really wanted it to look like was this:
<div class="call-feed blog"> <h4>Blog Roll Title</h4> <div class="listing"> <div class="title"> <a href="/blog/1">The Title</a> </div> <div class="description"> Lorem ipsum [...] </div> <div class="more-info-link"> <a href="/blog/1">more info></a> </div> </div> . . . </div>My new rewritten function looks like this:
function RSS_Display($url, $size)
{
global $RSS_Content;
$opened = false;
$page = "";
RSS_Retrieve($url);
if($size > 0) {
$recents = array_slice($RSS_Content, 0, $size);
}
foreach($recents as $article)
{
$type = $article["type"];
if($type != 0) {
$title = $article["title"];
$link = $article["link"];
$description = $article["description"];
$page .= '<div class="'.LISTINGDIV.'">'."\n";
$page .= '<div class="'.TITLEDIV.'">'.
'<a href="'.$link.'">'.
$title.'</a>'."</div>\n";
if($description != false)
{
$page .= '<div class="'.DESCDIV.'">'.
substr($description, 0, SUBSTRSIZE).
"[...]</div>\n";
}
$page .= '<div class="'.INFODIV.'"><a href="'.$link.'">'.
INFOTXT.'</a></div>'."\n";
$page .= "</div>\n";
}
}
return $page."\n";
}
As you may have noticed, I did not hardcode in the DIV class names (yay!). Instead, I have allowed you to change some define() values at the start of the file. Here is what they look like:
define('SUBSTRSIZE',130); // the max size of the description
define('LISTINGDIV','listing'); // <div> class surrounding listing
define('TITLEDIV','title'); // <div> class surrounding title
define('DESCDIV','synopsis'); // <div> class surrounding description
define('INFODIV','more-info-link'); // <div> class surrounding 'more info' link
define('INFOTXT','more >'); // text for 'more info' link
And after all of that, we have a MODx sidebar that incorporates a WordPress blogroll using RSS!
This project is for a customer and their blog has not gone live. Stay tuned for an update with their new blog soon!
Apologies for the formatting of the code. Some changes were made to the original code to ensure that the examples fit within the confines of the blog. If these adjustment have broken the code, my condolences. Here is the original file I used: rsslib.php - By Reed
Comments
Love your blog post but need some help
Hi
Thank you for your blog post, my shared host has blocked all securities in the form of fsockopen for a url ... is there anyway to modify your script that I can call the blog directly via the root instead of a url?
I truly appreciate your help
regards
Lance
Love your blog post but need some help
Hi
Thank you for your blog post, my shared host has blocked all securities in the form of fsockopen for a url ... is there anyway to modify your script that I can call the blog directly via the root instead of a url?
I truly appreciate your help
regards
Lance
Kaspar, I do not know
Kaspar,
I do not know exactly why you are getting the error you mention (as I did not write the RSS-reading code), but it looks like the code of the page you are using this feature on is attempting to use the same attribute name as one used in the RSS code. A simple Google search did not return any meaningful results, so you will likely have to debug the RSS and your own code.
Reed
Hey, when I follow your
Hey, when I follow your instructions then I get error @ http://lawyerpronto.com/testing2
DOMDocument::load() [domdocument.load]: Attribute value redefined in http://lawyerpronto.com/lawblog/feed, line: 37