Help needed with maxWords and image in RSS feed

3 posts by 3 authors in: Forums > CMS Builder
Last Post: June 13, 2017   (RSS)

By willydoit - June 12, 2017

Hi all,

I am attempting to create RSS feeds which will auto update social media sites, everything was going fine until I added  code to limit the number of words as I wanted to have viewers on social media to have to read the full article from our website. All was fine with that  except the maxWords resulted in the image being removed from the output.

As I see it I need the maxWords code to ignore any image present in its calculations or I need to use a seperate upload but if I use a seperate upload I dont know how to include it within the RSS output.

Any help would be appreciated.  I have attached my current RSS code below.

<?php
  /* STEP 1: LOAD RECORDS - Copy this PHP code block near the TOP of your page */
  
  // load viewer library
  $libraryPath = 'cmsb/lib/viewer_functions.php';
  $dirsToCheck = array('/home/sites/lpl-testsite.co.uk/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 from 'current_news'
  list($current_newsRecords, $current_newsMetaData) = getRecords(array(
    'tableName'   => 'current_news',
    'orderBy'     => 'Date',   // use default database order
    'loadUploads' => false,
    'allowSearch' => true,
    'where' => " category = 'Entertainment' ",
  ));

?>
<?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;
}
?>
<?php header('Content-type: application/xml; charset=utf-8'); ?><?php echo '<'.'?xml version="1.0" encoding="UTF-8"?>'; ?>
<rss version="2.0">
  <channel>
    <title>Bridlington Entertainment</title>
    <link>http://www.lpl-testsite.co.uk/</link>
    <description>Whats on Guide for Bridlington</description>
    <pubDate><?php echo date('r') ?></pubDate>
    <language>en-us</language>

    <?php foreach ($current_newsRecords as $record): ?>
    <item>
      <title><?php echo htmlencode($record['title']) ?></title>
      <link>http://<?php echo $_SERVER['HTTP_HOST']; ?><?php echo $record['_link'] ?></link>
      <description>  <![CDATA[<?php echo maxWords ($record['content'],60) ?> <a href="http://<?php echo $_SERVER['HTTP_HOST']; ?><?php echo $record['_link'] ?>" target="_blank">Read more..</a>]]></description>
      
      <pubDate><?php echo date('r', strtotime($record['createdDdate'])) ?></pubDate>
      <guid isPermaLink="false"><?php echo $record['_link'] ?></guid>
    </item>
    <?php endforeach ?>
  </channel>
</rss>

By AlanAlonso - June 13, 2017

That's easy just remove the image from the content field, and add a upload field just for the image.

<rss version="2.0">
  <channel>
    <title>Bridlington Entertainment</title>
    <link>http://www.lpl-testsite.co.uk/</link>
    <description>Whats on Guide for Bridlington</description>
    <pubDate><?php echo date('r') ?></pubDate>
    <language>en-us</language>

    <?php foreach ($current_newsRecords as $record): ?>
    <item>
      <title><?php echo htmlencode($record['title']) ?></title>
      <link>http://<?php echo $_SERVER['HTTP_HOST']; ?><?php echo $record['_link'] ?></link>
      <description>  <![CDATA[<?php echo maxWords ($record['content'],60) ?> <a href="http://<?php echo $_SERVER['HTTP_HOST']; ?><?php echo $record['_link'] ?>" target="_blank">Read more..</a>]]></description>
      <image>
    <url><?php echo $record['upload'] ?></url>
    <title><?php echo htmlencode($record['title']) ?></title>
    <link>http://<?php echo $_SERVER['HTTP_HOST']; ?><?php echo $record['_link'] ?></link>
  </image>

      <pubDate><?php echo date('r', strtotime($record['createdDdate'])) ?></pubDate>
      <guid isPermaLink="false"><?php echo $record['_link'] ?></guid>
    </item>
    <?php endforeach ?>
  </channel>
</rss>

By Damon - June 13, 2017

Hi,

I agree with Alan's suggestion to use a separate field for image upload to include in the RSS. 

If you already have images in the rich text editor, they are getting stripped out so they can be left in as is for the main site. The additional image upload field could be used just for including image content to the RSS feed.

Cheers,
Damon Edis - interactivetools.com

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