2011-09-28 104 views
2

我對RSS提要有疑問。我正在使用wordpress ver 3.2.1並在我的網站中使用其他網站rss feed。我的要求是明智地獲取Feed,並在我的網站側欄中顯示。如何從其他網站獲取月明智的rss feed

目前我使用的RSS窗口小部件,並顯示在側邊欄的飼料,但他們在下面的格式進來:

  1. 9-29星期四下午4:30基本任務 - 家長信息夜
  2. 週四10-6 - 全天 - 半天:小學會議

我會非常感謝您的幫助。提前致謝。

更新: 2012年3月13日

此代碼出現在的index.php

<h2><?php _e('Recent news from Some-Other Blog:'); ?></h2> 
<?php // Get RSS Feed(s) 
include_once(ABSPATH . WPINC . '/feed.php'); 

// Get a SimplePie feed object from the specified feed source. 
//$rss = fetch_feed('http://example.com/rss/feed/goes/here'); 

$rss = fetch_feed('http://lakewashington.intand.com/index.php?type=export&action=rss&schools=48&groups=884,876,874,996'); 

print_r($rss); 

if (!is_wp_error($rss)) : // Checks that the object is created correctly 
    // Figure out how many total items there are, but limit it to 5. 
    $maxitems = $rss->get_item_quantity(5); 

    // Build an array of all the items, starting with element 0 (first element). 
    $rss_items = $rss->get_items(0, $maxitems); 
endif; 
?> 

<ul> 
    <?php if ($maxitems == 0) echo '<li>No items.</li>'; 
    else 
    // Loop through each feed item and display each item as a hyperlink. 
    foreach ($rss_items as $item) : ?> 
    <li> 
     <a href='<?php echo esc_url($item->get_permalink()); ?>' 
     title='<?php echo 'Posted '.$item->get_date('j F Y | g:i a'); ?>'> 
     <?php echo esc_html($item->get_title()); ?></a> 
    </li> 
    <?php endforeach; ?> 
</ul> 

回答

1

你可以做手工。 只需使用wordpress native函數fetch_feed

你甚至有一個例子在那裏:

<h2><?php _e('Recent news from Some-Other Blog:'); ?></h2> 
<?php // Get RSS Feed(s) 
include_once(ABSPATH . WPINC . '/feed.php'); 

// Get a SimplePie feed object from the specified feed source. 
$rss = fetch_feed('http://example.com/rss/feed/goes/here'); 
if (!is_wp_error($rss)) : // Checks that the object is created correctly 
    // Figure out how many total items there are, but limit it to 5. 
    $maxitems = $rss->get_item_quantity(5); 

    // Build an array of all the items, starting with element 0 (first element). 
    $rss_items = $rss->get_items(0, $maxitems); 
endif; 
?> 

<ul> 
    <?php if ($maxitems == 0) echo '<li>No items.</li>'; 
    else 
    // Loop through each feed item and display each item as a hyperlink. 
    foreach ($rss_items as $item) : ?> 
    <li> 
     <a href='<?php echo esc_url($item->get_permalink()); ?>' 
     title='<?php echo 'Posted '.$item->get_date('j F Y | g:i a'); ?>'> 
     <?php echo esc_html($item->get_title()); ?></a> 
    </li> 
    <?php endforeach; ?> 
</ul> 

你只需把在你的sidebar.php,無論你需要顯示的飼料。

編輯行:

$rss = fetch_feed('http://example.com/rss/feed/goes/here'); 

...並指向的URL正確的飼料。 人和:

$maxitems = $rss->get_item_quantity(5); 

...指定多少內容,以顯示(五的例子)

然後檢查的foreach的UL裏面,在那裏你可以風格的飼料是如何顯示。

默認情況下,fetch_feed函數會將提要緩存12小時。 如果你需要每隔30天做一次,你可以使用wordpress的Transients API來完成,而不用麻煩。

+0

親愛的TCattd,首先我想說感謝您關注我的問題。我錯了 - 最近的新聞來自Some-Other博客: WP_Error Object([errors] => Array([simplepie-error] => Array([0] => WP HTTP錯誤:無法打開fopen()到http://lakewashington.intand.com/index.php?type=export&action=rss&schools=48&groups=884,876,874,996))[error_data] => Array()) *沒有項目。 – w3uiguru

+0

你能粘貼你使用的整個代碼嗎? – TCattd

+0

檢查我更新的代碼。 – w3uiguru