Show images in RSS feed / Also limited description length

13 posts by 6 authors in: Forums > CMS Builder
Last Post: September 10, 2010   (RSS)

By gadefgaertgqe - February 2, 2010 - edited: February 2, 2010

Hi There,

I am using your excellent 'Basic auto' template and have everything working including an RSS feed of the used bikes (as per instructions http://www.interactivetools.com/docs/cmsbuilder/rss_feeds.html).

However, there are 2 things I would like to be able to do:

1 - It is important for me to have an image with each item listed. I have tried all sorts of stuff but can't get it working. I know that I am very close as after following these instructions: http://www.interactivetools.com/docs/cmsbuilder/uploads.html) I can see the correct URL in the source! For some reason though it just isn't doing the last bit lol!

2 - Also is it possible to limit the length of a description to a certain amount of characters?

Many thanks in advance

8BG

Re: [8bit Gamer] Show images in RSS feed / Also limited description length

By gkornbluth - February 2, 2010 - edited: February 2, 2010

Hi 8BG,

RSS feeds can be finicky, but this should work to display an image.<description>
<![CDATA[

<p><u>Here is an image thumbnail</u> </p>

<?php foreach ($record['image'] as $upload): ?>
<img src="http://www.my_web_site.com<?php echo $upload['thumbUrlPath'] ?>" width="<?php echo $upload['thumbWidth'] ?>" height="<?php echo $upload['thumbHeight'] ?>" alt="" />
<?php endforeach ?></p>

<p align="center"><strong><font color="#ba1419">My First Image</font></strong></p>
]]>
</description>


In general, any time you want to treat code as "code" in an RSS feed, you need to surround the code with CDATA tags.

Note: You'll have to add the URL to change the relative image thumbURLpath location to absolute.

As far as limiting characters, here are 2 excerpts from my CMSB Cookbook that may help.

DISPLAYING THE FIRST X CHARACTERS ON A LISTING PAGE
Add this at the top of the page or before you want to invoke the character limiting function:

<?PHP function textLimit($string, $length, $replacer = '...')
{
if(strlen($string) > $length)
return (preg_match('/^(.*)\W.*$/', substr($string, 0, $length+1), $matches) ? $matches[1] : substr($string, 0, $length)) . $replacer;

return $string;
}

?>


and then where you want to display your content:

<?PHP echo textLimit($record['your_field'], 100) ?>

LIMITING THE NUMBER OF WORDS SHOWN IN A PAGE
If you use the character limiting function that’s exactly what you’ll get, and words can be cut off. But if you want to limit the word count, Dave shares his usual uncanny wisdom:

Put this code at the top of your page, or before you want to invoke the word limiting function:

<?PHP
function maxWords($textOrHtml, $maxWords) {
$text = strip_tags($textOrHtml);
$words = preg_split("/\s+/", $text, $maxWords+1);
if (count($words) > $maxWords) { unset($words[$maxWords]); }
$output = join(' ', $words);

return $output;
}
?>


Then put this code where you want the words limited (to 5 words):

<?PHP echo maxWords($record['content'], 5);
?>
If you want to add ...more and a link, use something like this:

<?PHP echo maxWords($record['content'], 5);
?>...<a href="<?php echo $record['_link']; ?>”>Read More</a>


Hope that gets you started.

Best,

Jerry Kornbluth
The first CMS Builder reference book is now available on-line!







Take advantage of a free 3 month trial subscription, only for CMSB users, at: http://www.thecmsbcookbook.com/trial.php

Re: [gkornbluth] Show images in RSS feed / Also limited description length

By gadefgaertgqe - February 9, 2010

That worked great! Thanks! :)

Re: [gkornbluth] Show images in RSS feed / Also limited description length

By rez - March 30, 2010

I am using the wysiwyg editor with barely any formatting at all but i do have several paragraphs. Limiting the words is making it all one paragraph. How can I keep my paragraphs?

Re: [gkornbluth] Show images in RSS feed / Also limited description length

By Jason - March 31, 2010

Hi rez,

Use this version of maxWords to keep your paragraph formatting.
<?PHP
function maxWords($textOrHtml, $maxWords) {
$text=str_replace("<p>","*P*",$textOrHtml);
$text= str_replace("</p>","*/P*",$text);
$text = strip_tags($text);
$words = preg_split("/\s+/", $text, $maxWords+1);
if (count($words) > $maxWords) { unset($words[$maxWords]); }
$output = join(' ', $words);
$output=str_replace("*P*","<p>",$output);
$output=str_replace("*/P*","</p>",$output);
$output.="</p>";

return $output;
}
?>



Let me know if that works for you.
---------------------------------------------------
Jason Sauchuk - Project Manager
interactivetools.com

Hire me! Save time by getting our experts to help with your project.
http://www.interactivetools.com/consulting/

Re: [Jason] Show images in RSS feed / Also limited description length

By dakrusha - September 10, 2010

i'm not getting this to work.
can somebody post a sample of where it is in the code?

thx
tim

Re: [dakrusha] Show images in RSS feed / Also limited description length

By Jason - September 10, 2010

Hi Tim,

What is the nature of the problem you're encountering? If you could clarify what's happening and attach the .php file you're working with, I can take a look for you.

Hope this helps.
---------------------------------------------------
Jason Sauchuk - Project Manager
interactivetools.com

Hire me! Save time by getting our experts to help with your project.
http://www.interactivetools.com/consulting/

Re: [Jason] Show images in RSS feed / Also limited description length

By dakrusha - September 10, 2010

it's not cutting down the amount of text displayed.
i'm sure i've done some thing wrong just not sure what.


<?php header('Content-type: text/html; charset=utf-8'); ?>
<?php


// load viewer library
$libraryPath = 'cmsAdmin/lib/viewer_functions.php';
$dirsToCheck = array('/hermes/bosweb/web251/b2519/sl.evsmc/public_html/','','../','../../','../../../');
foreach ($dirsToCheck as $dir) { if (@include_once("$dir$libraryPath")) { break; }}
if (!function_exists('getRecords')) { die("Couldn't load viewer library, check filepath in sourcecode."); }

// load records
list($about_usRecords, $about_usMetaData) = getRecords(array(
'tableName' => 'about_us',
));

?>

<?PHP
function maxWords($textOrHtml, $maxWords) {
$text = strip_tags($textOrHtml);
$words = preg_split("/\s+/", $text, $maxWords+1);
if (count($words) > $maxWords) { unset($words[$maxWords]); }
$output = join(' ', $words);

return $output;
}
?>

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8" />
<title>About EVS</title>
<style type="text/css">
<!--
body {
font-family: Verdana; background-image: url(http://www.equinevetspecialists.com/images/evs_background.jpg);
}

-->
</style></head>

<body>
<table width="800" border="0" align="center" cellpadding="0" cellspacing="0">
<tr>
<td align="center"><?php include 'header1.php'; ?></td>
</tr>
<tr>
<td align="center"><?php include 'menu.php'; ?></td>
</tr>
<tr>
<td><table width="972" border="0" cellspacing="0" cellpadding="0" background="http://www.equinevetspecialists.com/images/body_bg.jpg">
<tr>
<td><table width="972" border="0" cellspacing="5" cellpadding="0">
<tr>
<td height="700" valign="top">



<?php foreach ($about_usRecords as $record): ?>


<?php echo $record['content'] ?><br/>
<a href="<?php echo $record['_link'] ?>">Read More</a><br/>



<?php foreach ($record['images'] as $upload): ?>
<?PHP echo maxWords($record['content'], 5); ?>


<?php if ($upload['hasThumbnail']): ?>
<img src="<?php echo $upload['thumbUrlPath'] ?>" width="<?php echo $upload['thumbWidth'] ?>" height="<?php echo $upload['thumbHeight'] ?>" alt="" /><br/>

<?php elseif ($upload['isImage']): ?>
<img src="<?php echo $upload['urlPath'] ?>" width="<?php echo $upload['width'] ?>" height="<?php echo $upload['height'] ?>" alt="" /><br/>

<?php else: ?>
<a href="<?php echo $upload['urlPath'] ?>">Download <?php echo $upload['filename'] ?></a><br/>

<?php endif ?>
<?php endforeach ?>
<!-- STEP2a: /Display Uploads -->


<hr/>

<?php endforeach ?>
<?php if (!$about_usRecords): ?>
No records were found!<br/><br/>
<?php endif ?>





</td>
</tr>
</table></td>
</tr>
</table></td>
</tr>
<tr>
<td align="center"><?php include 'footer.php'; ?></td>
</tr>
</table>
</body>
</html>

Re: [dakrusha] Show images in RSS feed / Also limited description length

By Jason - September 10, 2010

Hi,

What's happening is that you're outputting the content of your about record in two different places:
<?php foreach ($about_usRecords as $record): ?>


<?php echo $record['content'] ?><br/>
<a href="<?php echo $record['_link'] ?>">Read More</a><br/>



<?php foreach ($record['images'] as $upload): ?>
<?PHP echo maxWords($record['content'], 5); ?>


You're only using the maxWords function the second time. Trying changing the code to this:
<?php foreach ($about_usRecords as $record): ?>


<?php echo maxWords($record['content'],5); ?><br/>
<a href="<?php echo $record['_link'] ?>">Read More</a><br/>


<?php foreach ($record['images'] as $upload): ?>


Hope this helps.
---------------------------------------------------
Jason Sauchuk - Project Manager
interactivetools.com

Hire me! Save time by getting our experts to help with your project.
http://www.interactivetools.com/consulting/