2012-07-10 135 views
0

我有2個網站,我希望使用RSS訂閱源從另一個展示一些帖子。以RSS訂閱源重新訂購商品

麻煩的是,默認似乎是按發佈日期排序,因爲我需要它們按標題排序。我使用Wordpress,它使用SimplePie(我相信這很常見?)。

有沒有辦法在顯示它們之前重新排序這些項目?謝謝。

/** 
* $feed = the RSS feed to display (set via CMS option) 
* $num_posts = the number of posts to display from the feed (set via CMS option) 
*/ 
$max_items = 0; 
if($feed !== '') : 
    $rss = fetch_feed($feed); 
    if(!is_wp_error($rss)) : 
     $max_items = $rss->get_item_quantity($num_posts); 
     $rss_items = $rss->get_items(0, $max_items); 
    endif; 
endif; 

回答

1

Okey,所以我想出了一個似乎工作的答案。

require_once(ABSPATH . WPINC . '/class-feed.php'); 
require_once(ABSPATH . WPINC . '/class-simplepie.php'); 

class SimplePie_Custom_Sort extends SimplePie{ 

    /** 
    * @var string How to order the feed 
    * @access private 
    */ 
    var $order_feed_by; 

    /** 
    * Sort the items that are to be displayed in an RSS feed 
    */ 
    function sort_items($a, $b){ 

     /** Construct the sort function name */ 
     $sort_function = 'sort_items_'.$this->order_feed_by; 

     /** Check if the sort function exists and call it (call 'parent::sort_items' if not) */ 
     if(method_exists($this, $sort_function)) : 
      $this->$sort_function($a, $b); 
     else : 
      parent::sort_items($a, $b); 
     endif; 

    } 

    /** 
    * Sort function to sort posts in an RSS feed by title 
    */ 
    function sort_items_title($a, $b){ 

     return $b->get_title() <= $a->get_title(); 

    } 

} 

function fetch_feed_custom($url, $order_by){ 

    $feed = new SimplePie_Custom_Sort(); 
    $feed->order_feed_by = $order_by; 
    $feed->set_feed_url($url); 
    $feed->set_cache_class('WP_Feed_Cache'); 
    $feed->set_file_class('WP_SimplePie_File'); 
    $feed->set_cache_duration(apply_filters('wp_feed_cache_transient_lifetime', 43200, $url)); 
    do_action_ref_array('wp_feed_options', array(&$feed, $url)); 
    $feed->init(); 
    $feed->handle_content_type(); 

    if($feed->error()) : 
     return new WP_Error('simplepie-error', $feed->error()); 
    endif; 

    return $feed; 

}