首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >如何从其他网站获取rss月wise

如何从其他网站获取rss月wise
EN

Stack Overflow用户
提问于 2011-09-28 15:46:05
回答 1查看 1.2K关注 0票数 2

我有一个关于RSS提要的问题。我正在使用wordpress 3.2.1和其他网站rss提要在我的网站。我的要求是获取的提要月明智,并显示在我的网站侧栏。

目前,我正在使用rss小部件,并在侧栏中显示提要,但它们的格式如下:

  1. 清华9-29下午4:30小学寻访-家长信息之夜
  2. 清华10-6 -All日-半天:小学会议

我将非常感谢你的帮助。提前谢谢。

更新:2012年3月13日

此代码存在于index.php中。

代码语言:javascript
复制
<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>
EN

回答 1

Stack Overflow用户

发布于 2012-03-09 16:09:22

你可以手动完成。只需使用wordpress本机函数馈送即可。

你甚至有一个例子:

代码语言:javascript
复制
<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中,只要显示提要即可。

编辑行:

代码语言:javascript
复制
$rss = fetch_feed('http://example.com/rss/feed/goes/here');

...and将url指向正确的提要。和:

代码语言:javascript
复制
$maxitems = $rss->get_item_quantity(5); 

...to指定要显示多少项(示例中有五项)

然后检查UL中的foreach,您可以在那里显示提要的样式。

默认情况下,fetch_feed函数将缓存提要12个小时。如果你需要每隔30天做一次,你可以使用wordpress的瞬态API来完成它,而不需要麻烦。

票数 1
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/7585871

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档