2018/07/18
投稿
備忘録です。
別WordPressの新着記事を読み込み表示する方法です。
参考サイト
複数WordPress運用時に別WordPressの新着記事を読み込み表示する
表示したいテンプレートに記述
<?php
include_once( ABSPATH . WPINC . '/feed.php' );
$feeduri = '取得するRSSの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 ) : ?>
<!-- 記事へのリンクを表示 -->
<a href="<?php echo $item->get_permalink(); ?>">
<!-- 記事の最初の画像を表示 -->
<?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="">
<!-- 記事タイトルを表示 -->
<?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; ?>
<!-- 投稿日を表示 -->
<?php
$item_date = $item->get_date();
$date = date('Y/m/d',strtotime( $item_date )); ?>
<p class="date"><?php echo $date; ?></p>
</a>
<?php
endforeach;
wp_reset_postdata();
?>
アイキャッチ画像を表示
functions.phpに記述
function rss_post_thumbnail($content) {
global $post;
if(has_post_thumbnail($post->ID)) {
$content = '<p>' . get_the_post_thumbnail($post->ID) . '</p>' . $content;
}
return $content;
}
add_filter('the_excerpt_rss', 'rss_post_thumbnail');
add_filter('the_content_feed', 'rss_post_thumbnail');
ゼヒトモ内でのプロフィール: ROCKSTREAM, ゼヒトモのホームページ作成・制作サービス, 仕事をお願いしたい依頼者と様々な「プロ」をつなぐサービス
2025/01/31
JQuery
2025/01/01
神社
御朱印
相模原
2024/10/27
ブラウザ
カスタム投稿
Wordpress
2024/08/20
神社
御朱印
2024/07/06
神社
御朱印