前提・実現したいこと
別ドメインのHTML内にwordpress記事をRSS取得、表示したいのですが、
画像の表示が強制的に代替画像が出てきてしまいます。
希望は、記事内の1枚目画像を取得したいです。(アイキャッチ設定はしていません)
参考にしたページ
https://kana-lier.com/web/php_rssfeed/
発生している問題・エラーメッセージ
投稿記事内の画像だけ取得できない
該当のソースコード
<?php require_once "./Feed.php" ; //rss-phpライブラリを読み込みます $feed = new Feed ; $url = "http://●●●●●.com/?feed=rss2"; //RSSのURLを入力する $rss = $feed->loadRss( $url ) ; $num = 3;//表示させたい件数 $i=0; foreach( $rss->item as $item ){ if($i>=$num){ } else{ $title = $item->title ; // タイトル $link = $item->link ; // リンク $timestamp = strtotime( $item->pubDate ) ; // 更新日時 $category = $item->category ; $description = $item->description ; // 詳細 $description = strip_tags($description); $content = $item->content ; if ( $desW != 0){ $description = mb_strimwidth($description, 0, $desW, "…",'utf-8'); } $thumbnail = trim($now_url,"/") . "/common/img/other/noimage.jpg";//画像がない場合の指定 if( preg_match_all('/<img([\s\S]+?)>/is', $item->content, $matches) ){ foreach( $matches[0] as $img ){ if ($img === reset($matches[0])) {//最初の画像にマッチしたもの if( preg_match('/src=[\'"](.+?jpe?g|png|gif)[\'"]/', $img, $m) ){ $item->$thumbnail = $m[1]; } } } } ?> <li> <a class="flex-wrap" href="<?php echo $link; ?>"> <span class="thumbnail"> <img src="<?php print $thumbnail; ?>" alt="<?php print $item->title; ?>" width="100"> </span><span class="news-category"><?php echo $category; ?></span>
<span class="news-date lightgray"><?php echo date( "Y年m月d日", $timestamp); ?></span>
<span class="news-title black"><?php echo $title; ?></span>
</a>
試したこと
以下、RSS記事の抜粋です。
discriptionに画像がなく、contentにあるので、thumbnailの条件内を$item->contentにしてみましたが、変わらずでした。
<guid isPermaLink="false">http://news.●●●.com/?p=75</guid>
<description><![CDATA[<slug>news</slug>これは投稿テスト文章です。]]></description>
content:encoded<![CDATA[<p>これは投稿テスト文章です。</p>
補足情報(FW/ツールのバージョンなど)
日付やタイトルなど、他のRSS情報は問題なく取得できています。
回答2件
あなたの回答
tips
プレビュー