22 September 2008 ~ 0 Comments

MagpieRSS: Truncating Titles with fetch_rss()

Truncating RSS titles for the fetch_rss() option of MagpieRSS turned out to be trickier than I initially expected. If you aren’t a hardcore php programmer and you’re just trying to fancy up your Wordpress blog, it can be a little obtuse as to how to achieve this. After a few Google searches turned up nothing I came up with this solution…

<h3>Title of Your RSS Feed</h3>
<?php
// assign the feed to a variable named 'feed'
$feed = 'http://url-of-your-rss-feed.com/feed/';
	if ($feed) {
		include_once(ABSPATH . WPINC . '/rss.php');
// define that 'feed' is also your url
$url = $feed;
$rss = fetch_rss($feed);
// string to return if feed fails
 	if ($rss == false){
$string .= "[No Feed To Retrieve]";
	return $string;
}
$maxitems = 10;
$items = array_slice($rss->items, 0, $maxitems);
	foreach ( $items as $item ) :
// define the string which is '$title'
// define the action 'substr' means sub-string or 'part of a string'
// define where the feed string starts - first letter is '0'
// define the length of the feed string - 55 characters
// define the trailing characters - "..."
$title = substr($item['title'],0,55)." ... ";
?>
<ul>
<li>
// link to the rss item origin with the description as the title
<a href='<?php echo $item['link']; ?>' title='<?php echo $item['description']; ?>'>
// print the truncated RSS title
<?php print $title; ?>
</a></li>
</ul>
<?php endforeach;
}
?>
<br /><br />

This code will allow you to truncate and print the titles of an rss feed. No more worries about whether or not that aggregated feed from Twitter or another blog will break your design.

This website uses IntenseDebate comments, but they are not currently loaded because either your browser doesn't support JavaScript, or they didn't load fast enough.

Leave a Reply

Bad Behavior has blocked 803 access attempts in the last 7 days.