You are here:  » Display latest 2 posts from Feed


Display latest 2 posts from Feed

Submitted by Iain Norman on Wed, 2015-01-07 15:21 in

Hi,

I am trying to display the last 2 items from a WordPress RSS Feed. I have tried a few of the solutions on the forum however these don't seem to work in this situation. I only seem to get a single item. I have included the code so far if someone could suggest how to do this.

Thanks

<?php
  require("MagicParser.php");
$counter = 0;
  function myRecordHandler($record)
  {
global $counter;
    // The following code will print out each field in your sample data:
    ?>
<div id="news_box"><div class="title"><a target="_parent" href="<?php print $record["CHANNEL/ITEM/LINK"];?>"><?php print $record["CHANNEL/ITEM/TITLE"];?></a></div>
    <div class="description"><?php print "<span>".substr($record["CHANNEL/ITEM/DESCRIPTION"],0,400)."...</span>";?> <a target="_parent" href="<?php print $record["CHANNEL/ITEM/LINK"];?>">Read More</a></div>
    </div><?php
$counter++;
    return ($counter == 2);
  }
  MagicParser_parse("http://domain.com/feed/?cat=8","myRecordHandler","xml|RSS/");
?>

Submitted by support on Wed, 2015-01-07 16:50

Hello Iain,

To display the last 2 items in a feed, since it is not (definitely) known how many items are present before the parse, the easiest thing to do is to parse the records into a global array, and then pick-off the last 2 items in the array for display.

Have a go with something like this:

<?php
  require("MagicParser.php");
  $records = array();
  function myRecordHandler1($record)
  {
    global $records;
    $records[] = $record;
  }
  MagicParser_parse("http://domain.com/feed/?cat=8","myRecordHandler1","xml|RSS/");
  function myRecordHandler2($record)
  {
    ?>
<div id="news_box"><div class="title"><a target="_parent" href="<?php print $record["CHANNEL/ITEM/LINK"];?>"><?php print $record["CHANNEL/ITEM/TITLE"];?></a></div>
<div class="description"><?php print "<span>".substr($record["CHANNEL/ITEM/DESCRIPTION"],0,400)."...</span>";?> <a target="_parent" href="<?php print $record["CHANNEL/ITEM/LINK"];?>">Read More</a></div>
</div>
     <?php
  }
  for($i=0;$i<2;$i++)
  {
    $record = array_pop($records);
    myRecordHandler2($record);
  }
?>

Hope this helps!
Cheers,
David
--
MagicParser.com

Submitted by Iain Norman on Wed, 2015-01-07 17:09

Hi David,

Thanks for getting back to me so quickly!

I got it working in the end using the code I posted but I removed CHANNEL/ITEM/ from the records and put that after the record handler and URL and it all works now.

<?php
  require("MagicParser.php");
$counter = 0;
  function myRecordHandler($record)
  {
global $counter;
    // The following code will print out each field in your sample data:
    ?>
<div id="news_box"><div class="title"><a target="_parent" href="<?php print $record["LINK"];?>"><?php print $record["TITLE"];?></a></div>
    <div class="description"><?php print "<span>".substr($record["DESCRIPTION"],0,400)."...</span>";?> <a target="_parent" href="<?php print $record["LINK"];?>">Read More</a></div>
    </div><?php
$counter++;
    return ($counter == 2);
  }
  MagicParser_parse("http://domain.com/feed/?cat=8","myRecordHandler","xml|RSS/CHANNEL/ITEM/");
?>

Submitted by support on Wed, 2015-01-07 17:11

No worries!

Glad you're up and running!

Cheers,
David
--
MagicParser.com