如何在WordPress博客上显示任何RSS源

有时您希望在博客上显示外部RSS源。也许是您的另一个博客或其他网站的博客供稿。好吧,你不需要一个插件来做这个,因为WordPress有一个内置的功能,将负责这一点。在本文中,我们将向您展示如何在博客上显示外部RSS源。这样你甚至可以使用WordPress作为新闻聚合器。

只需将以下代码粘贴到您选择的任何WordPress文件中即可。最好是在您创建的自定义页面中。

  <h5><?php _e( "Recent news from Some-Other Blog:", "my-text-domain" ); ?></h5>    <?php // Get RSS Feed(s)  include_once( ABSPATH . WPINC . "/feed.php" );    // Get a SimplePie feed object from the specified feed source.  $rss = fetch_feed( "https://www.wpbeginner.com/feed/" );    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 ) : ?>          <li><?php _e( "No items", "my-text-domain" ); ?></li>      <?php else : ?>          <?php // Loop through each feed item and display each item as a hyperlink. ?>          <?php foreach ( $rss_items as $item ) : ?>              <li>                  <a href="<?php echo esc_url( $item->get_permalink() ); ?>"                      title="<?php printf( __( "Posted %s", "my-text-domain" ), $item->get_date("j F Y | g:i a") ); ?>">                      <?php echo esc_html( $item->get_title() ); ?>                  </a>              </li>          <?php endforeach; ?>      <?php endif; ?>  </ul>  

确保更改Feed网址和数量以及您喜欢的任何其他设置。

有时您希望在博客上显示外部RSS源。也许是您的另一个博客或其他网站的博客供稿。好吧,你不需要一个插件来做这个,因为WordPress有一个内置的功能,将负责这一点。在本文中,我们将向您展示如何在博客上显示外部RSS源。这样你甚至可以使用WordPress作为新闻聚合器。

只需将以下代码粘贴到您选择的任何WordPress文件中即可。最好是在您创建的自定义页面中。

  <h5><?php _e( "Recent news from Some-Other Blog:", "my-text-domain" ); ?></h5>    <?php // Get RSS Feed(s)  include_once( ABSPATH . WPINC . "/feed.php" );    // Get a SimplePie feed object from the specified feed source.  $rss = fetch_feed( "https://www.wpbeginner.com/feed/" );    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 ) : ?>          <li><?php _e( "No items", "my-text-domain" ); ?></li>      <?php else : ?>          <?php // Loop through each feed item and display each item as a hyperlink. ?>          <?php foreach ( $rss_items as $item ) : ?>              <li>                  <a href="<?php echo esc_url( $item->get_permalink() ); ?>"                      title="<?php printf( __( "Posted %s", "my-text-domain" ), $item->get_date("j F Y | g:i a") ); ?>">                      <?php echo esc_html( $item->get_title() ); ?>                  </a>              </li>          <?php endforeach; ?>      <?php endif; ?>  </ul>  

确保更改Feed网址和数量以及您喜欢的任何其他设置。

相关推荐

发表回复

房先生
我们将24小时内回复。
2024-05-10 13:01:51
您好,有任何疑问请与我们联系!
您的工单我们已经收到,我们将会尽快跟您联系!
[QQ客服]
2781198
加我微信
[电话联系]
13585372227
[电子邮件]
chaoneo@163.com
取消

选择聊天工具: