i found code displaying wordpress posts on page rss feed. has limit display 5 posts.
<?php $rss = new domdocument(); $rss->load('http://wordpress.org/news/feed/'); $feed = array(); foreach ($rss->getelementsbytagname('item') $node) { $item = array ( 'title' => $node->getelementsbytagname('title')->item(0)->nodevalue, 'desc' => $node->getelementsbytagname('description')->item(0)->nodevalue, 'link' => $node->getelementsbytagname('link')->item(0)->nodevalue, 'date' => $node->getelementsbytagname('pubdate')->item(0)->nodevalue, ); array_push($feed, $item); } $limit = 5; for($x=0;$x<$limit;$x++) { $title = str_replace(' & ', ' & ', $feed[$x]['title']); $link = $feed[$x]['link']; $description = $feed[$x]['desc']; $date = date('l f d, y', strtotime($feed[$x]['date'])); echo '<p><strong><a href="'.$link.'" title="'.$title.'">'.$title.'</a></strong><br />'; echo '<small><em>posted on '.$date.'</em></small></p>'; echo '<p>'.$description.'</p>'; } ?>
i increase $limit "50" example if have less 50 posts show me error.
my guess strip out these lines seems stop script functioning:
$limit = 5; for($x=0;$x<$limit;$x++)
this should do. limit items contained in feed.
$limit = count($feed);
i wrap in if
ensure has items.
$limit = count($feed); if ($limit > 0) { for($x=0;$x<$limit;$x++) { $title = str_replace(' & ', ' & ', $feed[$x]['title']); $link = $feed[$x]['link']; $description = $feed[$x]['desc']; $date = date('l f d, y', strtotime($feed[$x]['date'])); echo '<p><strong><a href="'.$link.'" title="'.$title.'">'.$title.'</a></strong><br />'; echo '<small><em>posted on '.$date.'</em></small></p>'; echo '<p>'.$description.'</p>'; } }
Comments
Post a Comment