Advertisement
sabbirshouvo

Simple RSS GRAB WORDPRESS

Sep 21st, 2016
89
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
PHP 1.23 KB | None | 0 0
  1. <?php
  2.  
  3. function readRss($atts) {
  4.     extract(shortcode_atts(array(
  5.         "feed" => '',
  6.         "num" => '',
  7.     ), $atts));
  8.    
  9.     $feed2 = $atts['feed'];
  10.     $num = $atts['num'];
  11.    
  12.     $rss = new DOMDocument();
  13.     // $rss->load('http://wordpress.org/news/feed/');
  14.     $rss->load($feed2);
  15.     $feed = array();
  16.     foreach ($rss->getElementsByTagName('item') as $node) {
  17.         $item = array (
  18.             'title' => $node->getElementsByTagName('title')->item(0)->nodeValue,
  19.             'desc' => $node->getElementsByTagName('description')->item(0)->nodeValue,
  20.             'link' => $node->getElementsByTagName('link')->item(0)->nodeValue,
  21.             'date' => $node->getElementsByTagName('pubDate')->item(0)->nodeValue,
  22.             );
  23.         array_push($feed, $item);
  24.     }
  25.     $limit = $num;
  26.     $output = '';
  27.     foreach(array_slice($feed, 0, $limit) as $feeditem ) {
  28.         $title = str_replace(' & ', ' &amp; ', $feeditem['title']);
  29.         $link = $feeditem['link'];
  30.         $description = $feeditem['desc'];
  31.         $date = date('l F d, Y', strtotime($feeditem['date']));
  32.        
  33.         $output .= '<p><strong><a href="'.$link.'" title="'.$title.'">'.$title.'</a></strong><br />';
  34.         $output .= '<small><em>Posted on '.$date.'</em></small></p>';
  35.         $output .= '<p>'.$description.'</p>';
  36.  
  37.     }
  38.     return $output;
  39. }  
  40. add_shortcode('rssfeed', 'readRss');
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement