2011-09-28 106 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