PDA

View Full Version : RSS Display boxes - Limiting Description Length



creative99
11-25-2010, 03:49 PM
1) Script Title: Rss Display Boxes

2) Script URL (on DD): http://www.dynamicdrive.com/dynamicindex18/rssdisplaybox/index.htm

3) Describe problem: Hi I am trying to find a way to limit the RSS feed description length that is displayed. Some of my posts are quite long and I want to prevent them flooding my display box.

Can someone please tell me how to limit the feed description length using the RSS display box script.

Thanks!

jscheuer1
11-25-2010, 06:02 PM
Replace the outputbody.php file with this version:


<?php
//Function for ouputting the body of each RSS item displayed (inside loop)- DynamicDrive.com
//For syntax pf bpdu, see: http://simplepie.org/docs/installation/from-scratch/ and http://simplepie.org/docs/reference/
//Function by default defines 3 different body outputs (templates). Modify or add additional templates as desired

function shortencontent($content, $len=30){
if(strlen($content) > $len){
return substr($content, 0, $len - 6) . ' . . .';
} else {
return $content;
}
}

function outputbody($item, $template=""){
if ($template=="" || $template=="default"){ //DEFAULT TEMPLATE
?>
<DIV class="rsscontainer">
<div class="rsstitle"><a href="<?php echo $item->get_permalink(); ?>"><?php echo $item->get_title(); ?></a></div>
<div class="rssdate"><?php echo $item->get_date('d M Y g:i a'); ?></div>
<div class="rssdescription"><?php echo shortencontent($item->get_description()); ?></div>
</DIV>
<?php
} //end default template
else if ($template=="titles"){ //"TITLES" TEMPLATE
?>
<DIV class="rsscontainer">
<div class="rsstitle"><a href="<?php echo $item->get_permalink(); ?>" target="_new"><?php echo $item->get_title(); ?></a></div>
<div>Category: <?php echo $item->get_category(); ?></div>
</DIV>
<?php
} //end titles template
else if ($template=="titlesdates"){ //"TITLESDATES" TEMPLATE
?>
<DIV class="rsscontainer">
<span class="rsstitle"><a href="<?php echo $item->get_permalink(); ?>"><?php echo $item->get_title(); ?></a></span>
<span class="rssdate"><?php echo $item->get_date('m/d/y g:i a'); ?></span>
</DIV>
<?php
} //end titlesdates template
else if ($template=="mytemplatename"){ //"mytemplatename" TEMPLATE
?>
//DEFINE ADDITIONAL CUSTOM TEMPLATE(s) USING SAME LOGIC STRUCTURE AS ABOVE
//For syntax of template body, see SimplePie docs: http://simplepie.org/docs/installation/from-scratch/ and http://simplepie.org/docs/reference/
<?php
}




else
die ("No template exists with such name!");
} //Closing function bracket
?>

Notice the red 30 near the beginning. It determines the maximum length for the content, the description in this case. All descriptions longer than that will have their last 6 characters up to 30 replaced with:


. . .

and the rest dropped. Obviously 30 characters are probably too short, so adjust that red number as desired. It will be the maximum length including the padding dots of any description. Any description shorter or equal to this number in length will appear in its entirety without the padding dots.

Notes:

I also took the liberty of replacing all shorthand <? tokens with the more formal <?php one. This will have no bearing on the functionality. It simply brings outputbody.php into line with the other PHP scripts used by RSS Box and will avoid errors on systems that don't accept the shortcut token.

If you want to use the shortencontent function for other stuff in outputbody.php, but with a different length, specify that length in the call, example:


<?php echo shortencontent($item->get_title(), 15); ?>

creative99
11-25-2010, 06:22 PM
Thanks so much John,

It now works perfectly. I changed the length to 400 and it cut the paragraph nicely in half.

However, I now have a new problem:( I originally created an additional CSS class in the rssdisplaybox.css named .rssblogdescription. this was so I can format this feed differently to others on my page. All the text formatting has not been affected, but the css for images in this class is now being ignored. Here is a copy of the bit of code that related to images in that specific feed.
It set images to a specific size, took off the border, and had a nice text wrap around it. Now the images are full size, with a border and no text wrap.

.rssblogdescription img {float:left;
margin-right: 12px;
margin-top: 0px;
margin-left: 0px;
height: 60px;
width: 60px;
margin-bottom: 5px;
border: 0px;
display:inline;
background:no-repeat; }

this must be something to do with the changes I have made in the outputbody.php

Any suggestions?

Many thanks again for your help with this.

creative99
11-25-2010, 06:28 PM
Forget that John, I have sorted it. It was because I had named the template to rssblogdescription instead:-)

Thanks again for your help with this! you are a lifesaver:-))