大峰笔记
  • 关于
  • 搜索

调用rss的第一条 Sat, Jun 22, 2024

突发奇想,我想把memos的第一条显示在博客的首页,经过两天的折腾,这个想法算是实现了,调用rss的第一篇文章显示在wordpress的首页,但是,有个缓存时间,也就是说,当我发布memos后,经过大约1小时左右,博客首页的rss调用才会同步更新,这个应该跟memos没关系,因为换rss地址为其它后也是这样,不知道跟simplepie库有没有关系,或者其它,目前先这样玩玩。

<?php
// 获取RSS提要的URL
$rss_url = 'http://yourwebsite.com/feed';

// 使用simplepie库解析RSS提要
include_once( ABSPATH . WPINC . '/class-simplepie.php' );
$feed = new SimplePie();
$feed->set_feed_url( $rss_url );
$feed->init();
$feed->handle_content_type();

// 如果有至少一篇文章,则显示第一篇
if ( $feed->get_item_quantity() > 0 ) {
    $first_item = $feed->get_item(0);
    ?>
    <h2><a href="<?php echo $first_item->get_permalink(); ?>"><?php echo $first_item->get_title(); ?></a></h2>
<p><?php echo $first_item->get_description(); ?></p>
<?php
} else {
    // 没有找到文章
    echo 'No posts found.';
}
?>

较新的一篇: Wordpress首页显示最近的评论者头像
较旧的一篇: 20240616

Back to Home | bXpmOTk5QHFxLmNvbQ== | TOP ↑
hugo.386 | © 2025 | 苏ICP备2022047275号-1,苏公网安备 32020602001142号