別ドメインで運用しているサイト(静的html)の新着情報を、表示させたいのですが、うまく表示されません。
2案作ってみたのでソースを貼ります。運用するにあたり、どちらの案で掲載しても大丈夫です。
1つ目のエラーをみていると、「ABSPATH」と「WPINC」が原因だと思いました。ちなみに、別ドメインでもワードプレスに貼るときれいに動きました。
Warning: Use of undefined constant ABSPATH - assumed 'ABSPATH' (this will throw an Error in a future version of PHP)
Warning: Use of undefined constant WPINC - assumed 'WPINC' (this will throw an Error in a future version of PHP)
2つ目は何が原因かわかりませんが、運用しているサイトのcssまで読み取っていたり、記事を3つまでと制限したいところ全て出力していました。
▼参考サイト
https://idotdesign.net/blog/web/wordpress/using-fetchfeed/
▼1つ目
<?php include_once( ABSPATH . WPINC . '/feed.php' ); $feeduri = 'feedのURL'; $rss = fetch_feed($feeduri); if (!is_wp_error($rss)) { $maxitems = $rss->get_item_quantity(3); $rss_items = $rss->get_items( 0, $maxitems ); } foreach ( $rss_items as $item ) : ?> <!-- 記事へのリンクを表示 --> <div class="box_article"> <div class="box_news"> <a href="<?php echo $item->get_permalink(); ?>" target="_blank" class="cardtype__link" data-wpel-link="internal"> <!-- 記事の最初の画像を表示 --> <?php $first_img = ''; if ( preg_match( '/<img.+?src=[\'"]([^\'"]+?)[\'"].*?>/msi', $item->get_content(), $matches ) ) { $first_img = $matches[1]; } ?> <img src="<?php echo esc_attr( $first_img ); ?>" alt="" class="box_img"> <!-- 投稿日を表示 --> <div class="box_write"> <?php $item_date = $item->get_date(); $date = date('Y年m月d日',strtotime( $item_date )); ?> <p class="date"> <?php echo $date; ?> </p> <!-- 記事タイトルを表示 --> <?php $title = $item->get_title(); if(mb_strlen( $title ) > 40 ): ?> <p class="title"><?php echo mb_substr( $title,0,40 );?>...</p> <?php else : ?> <p class="title"><?php echo $title ;?></p> <?php endif; ?> </div> </a> </div><!----class="box_news"------> </div> <?php endforeach; wp_reset_postdata(); ?>
▼2つ目
<?php require_once "./feed.php" ; $feed = new Feed ; $url = "feedのURL"; $rss = $feed; $num = 3; $i=0; $desW = 0; foreach ( $rss_items as $item ) : ?> <!-- 記事へのリンクを表示 --> <div class="box_article"> <div class="box_news"> <a href="<?php echo $item->get_permalink(); ?>" target="_blank" class="cardtype__link" data-wpel-link="internal"> <!-- 記事の最初の画像を表示 --> <?php $first_img = ''; if ( preg_match( '/<img.+?src=[\'"]([^\'"]+?)[\'"].*?>/msi', $item->get_content(), $matches ) ) { $first_img = $matches[1]; } ?> <img src="<?php echo esc_attr( $first_img ); ?>" alt="" class="box_img"> <!-- 投稿日を表示 --> <div class="box_write"> <?php $item_date = $item->get_date(); $date = date('Y年m月d日',strtotime( $item_date )); ?> <p class="date"> <?php echo $date; ?> </p> <!-- 記事タイトルを表示 --> <?php $title = $item->get_title(); if(mb_strlen( $title ) > 40 ): ?> <p class="title"><?php echo mb_substr( $title,0,40 );?>...</p> <?php else : ?> <p class="title"><?php echo $title ;?></p> <?php endif; ?> </div> </a> </div><!----class="box_news"------> </div> <?php endforeach; wp_reset_postdata(); ?>
どうぞよろしくお願いします。
あなたの回答
tips
プレビュー