dragunoff

[WP] SImplePie Feed Parsing Function

May 22nd, 2011
336
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
PHP 1.39 KB | None | 0 0
  1.  
  2. <?php
  3.  
  4. //This file is needed to be able to use feed parsing functions.
  5. include_once( ABSPATH . WPINC . '/feed.php' );
  6.  
  7. // parse any RSS feed
  8. function custom_fetch_feed( $args = '' ) {
  9.  
  10.     // default arguments (additional ones can be added at will)
  11.     $defaults = array(
  12.         'url' => '', // feed url
  13.         'num' => 7, // max items to display
  14.          );
  15.        
  16.     // Parse incomming $args into an array and merge it with $defaults
  17.     $args = wp_parse_args( $args, $defaults );
  18.    
  19.     // OPTIONAL: Declare each item in $args as its own variable i.e. $type, $before.
  20.     extract( $args, EXTR_SKIP );
  21.    
  22.     // Get a SimplePie feed object from the specified feed source.
  23.     $feed = fetch_feed( $url );
  24.  
  25.         // Checks that the object is created correctly
  26.     if ( !is_wp_error( $feed ) ) {
  27.    
  28.         // Figure out how many total items there are, but limit it to parameter.
  29.         $maxitems = $feed->get_item_quantity( $num );
  30.  
  31.         // Build an array of all the items, starting with element 0 ( first element ).
  32.         $feed_items = $feed->get_items( 0, $maxitems );
  33.        
  34.     }
  35.    
  36.     // if there are no item in the feed
  37.     if ( $maxitems == 0 ) {
  38.    
  39.         $content = '<!-- No items -->';
  40.        
  41.     } else {
  42.    
  43.         $content = '';
  44.    
  45.         // Loop through each feed item and display each item as a hyperlink.
  46.         foreach ( $feed_items as $item ) :
  47.        
  48.             $content .= '<!-- do whatever with feed object -->';
  49.            
  50.         endforeach;
  51.    
  52.     }
  53.    
  54.     return $content;
  55.    
  56. }
Advertisement
Add Comment
Please, Sign In to add comment