You are here:  » RSS Feed Help

Support Forum



RSS Feed Help

Submitted by dvsbruce on Fri, 2010-05-21 21:12 in

I am using the following code for my RSS feed. I got the code from another post on this forum but I can't remember where. My feed is displaying 20 random products with descriptions. I would like to change the feed to only display the first 200 or so characters in the description. Also I would like to add a publication date to each post. Is there any way to do this?

Here is the code I am using:

<?php
  $baseURL = "{link saved}"; // URL of your site without trailing "/"
  require("includes/common.php");
  header("Content-Type: text/xml");
  print "<?xml version='1.0' encoding='UTF-8'?>";
  print "<rss version='2.0'>";
  print "<channel>";
  print "<title>Camp, Fish, Hunt, Compare</title>";
  print "<link>".$baseURL.$config_baseHREF."</link>";
  print "<description>Find the cheapest prices on name brand outdoor products.</description>";
  $sql = "SELECT * FROM `".$config_databaseTablePrefix."products` ORDER BY RAND() LIMIT 20";
  if (database_querySelect($sql,$rows))
  {
    foreach($rows as $product)
    {
      print "<item>";
      if ($config_useRewrite)
      {
        $href = "product/".tapestry_hyphenate($product["name"]).".html";
      }
      else
      {
        $href = "products.php?q=".urlencode($product["name"]);
      }
      print "<title>".$product["name"]."</title>";
      print "<link>".$baseURL.$config_baseHREF.$href."</link>";
      print "<guid>".$baseURL.$config_baseHREF.$href."</guid>";
      print "<description><![CDATA[";
      print "<p>".$product["description"]."</p>";
      print "]]></description>";
      print "</item>";
    }
  }
  print "</channel>";
  print "</rss>";
?>

Thanks,

Bruce

Submitted by support on Sat, 2010-05-22 03:13

Hi Bruce,

Sure - a neat way to truncate the description is to make sure that it is broken on a space rather than mid-word. To do this, look for the following code in your rss.php:

print "<p>".$product["description"]."</p>";

...and REPLACE that line with:

$breakLimit = 200;
if (strlen($product["description"]) > $breakLimit)
{
  $breakOffset = strpos($product["description"]," ",$breakLimit);
  if ($breakOffset !== false)
  {
    $product["description"] = substr($product["description"],0,$breakOffset);
  }
}
print "<p>".$product["description"]."</p>";

To add a pubDate element (which is a channel level element), you can use the "r" format of PHP's date() function which returns an RFC822 date/time. Look for the following line in your code:

print "<channel>";

...and REPLACE with:

print "<channel>";
print "<pubDate>".date("r")."</pubDate>";

Hope this helps!

Cheers,
David.

Submitted by dvsbruce on Sat, 2010-05-22 07:13

You're the Best! Works perfectly...Thanks!

Bruce