WordPressで外部ブログのRSSを取得して記事一覧を表示する

WordPress
スポンサーリンク
<?php include_once( ABSPATH . WPINC . '/feed.php' );
$rss = fetch_feed( 'https://bstyleworks.com/' );
$maxitems = 0;
if ( ! is_wp_error( $rss ) ) : 
    $maxitems = $rss->get_item_quantity( 1 ); 
    $rss_items = $rss->get_items( 0, $maxitems );
endif;
?>
<?php if ( $maxitems == 0 ) : ?>
  ブログ記事はありません。
<?php else : ?>
  <?php foreach ( $rss_items as $item ) : ?>
    <?php echo esc_html( $item->get_date('Y.m.d') ); ?>
    <a target="_blank" href="<?php echo esc_url( $item->get_permalink() ); ?>">
    <?php echo esc_html( $item->get_title() ); ?></a>
  <?php endforeach; ?>
<?php endif; ?>

fetch_feed( 'https://bstyleworks.com/' ); の部分は、外部ブログのURLに指定し直してください。

コメント

タイトルとURLをコピーしました