如何从其他网站每月获取 RSS feed

发布于 2024-12-06 20:53:04 字数 1641 浏览 0 评论 0原文

我有一个关于 RSS 源的问题。我正在使用 WordPress 版本 3.2.1 并在我的网站中使用其他网站的 RSS 提要。我的要求是按月获取提要并显示在我的网站侧栏中。

目前我正在使用 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>

I have a question regarding RSS feeds. I am using wordpress ver 3.2.1 and using other website rss feeds in my website. My requirement is to fetch the feeds month wise and to show in my website side bar.

Currently i am using rss widget and showing the feeds in sidebar but they are coming in below format:

  1. Thu 9-29 4:30pm Elementary Quest - Parent Information Night
  2. Thu 10-6 -All Day- Half-Day: Elementary Conferences

I will be very thankful for your help. Thanks in advance.

Update: Mar 13 2012

This code is present in 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>

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

我们的影子 2024-12-13 20:53:04

您可以手动完成。
只需使用 WordPress 原生函数 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 指向正确的 feed。
并且:

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

...指定要显示的项目数(示例中为五个)

然后检查 UL 内的 foreach,您可以在其中设置 Feed 的显示方式。

默认情况下,fetch_feed 函数会将 feed 缓存 12 小时。
如果您需要每 30 天执行一次,您可以使用 wordpress 的 Transients API 轻松完成此操作。

You can do it manually.
Just use wordpress native function fetch_feed.

You even have an example in there:

<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>

You just need to put that in your sidebar.php, wherever you need to show the feed.

Edit the line:

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

...and point the url to the correct feed.
And:

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

...to specify how many items to show (five in the example)

Then check the foreach inside the UL, there you can style how the feed are shown.

By default, fetch_feed function will cache the feeds for 12 hours.
If you need to do it every 30 days, you can use the wordpress's Transients API to accomplish that without hassle.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文