Skip to content

Instantly share code, notes, and snippets.

@osakin
Created December 26, 2024 00:58
Show Gist options
  • Save osakin/14a0808e3d1d5c2225edb96cef753ecc to your computer and use it in GitHub Desktop.
Save osakin/14a0808e3d1d5c2225edb96cef753ecc to your computer and use it in GitHub Desktop.
WordPress REST APIを使用して他のWordPressサイトの記事を取得するコード(他のWordPress上の記事の更新情報を別のWordPress上で表示する)
// WordPress上で他のWordPressにある記事を取得し、表示するコード
// このコードは子テーマの functions.php に追加します。
// 表示させたい箇所でショートコード [remote_posts] を使用してください。
function fetch_posts() {
// REST APIから投稿を取得するURLを指定します。
// 現在は15件の最新投稿を取得する設定になっています。
// カテゴリや記事IDを指定したい場合はURLに追加します(後述)。
$response = wp_remote_get('https://mydomain.com/wp-json/wp/v2/posts?_embed&per_page=15');
// リクエストがエラーの場合の処理
if (is_wp_error($response)) {
return 'エラーが発生しました';
}
// 取得したデータをJSON形式からデコード
$posts = json_decode(wp_remote_retrieve_body($response));
// HTMLリストを生成する準備
$output = '<ul class="wp-block-latest-posts__list wp-block-latest-posts">';
// 投稿データをループで処理
$count = 0; // 投稿数をカウントする変数
foreach ($posts as $post) {
$count++;
// 奇数・偶数でCSSクラスを付与
$class = ($count % 2 === 0) ? 'even' : 'odd';
// アイキャッチ画像のURLを取得
$featured_image = '';
if (isset($post->_embedded->{'wp:featuredmedia'}[0]->source_url)) {
$featured_image = $post->_embedded->{'wp:featuredmedia'}[0]->source_url;
}
// リストアイテムの開始タグ
$output .= '<li class="' . esc_attr($class) . '">';
// 最初の10件にはアイキャッチ画像を表示
if ($count <= 10 && $featured_image) {
$output .= '<div class="wp-block-latest-posts__featured-image">';
$output .= '<img src="' . esc_url($featured_image) . '" class="attachment-large size-large wp-post-image" alt="' . esc_attr($post->title->rendered) . '" decoding="async" loading="lazy">';
$output .= '</div>';
}
// 記事のタイトルとリンクを生成
$output .= '<a class="wp-block-latest-posts__post-title" href="' . esc_url($post->link) . '">';
$output .= esc_html($post->title->rendered);
$output .= '</a>';
// リストアイテムの終了タグ
$output .= '</li>';
}
// HTMLリストの終了タグ
$output .= '</ul>';
// 完成したHTMLを返す
return $output;
}
// ショートコードを登録
// このショートコード [remote_posts] を使用すると、記事リストが表示されます。
add_shortcode('remote_posts', 'fetch_posts');
> **タイトル:**
> WordPress REST APIを使用して他のWordPressサイトの記事を取得するコード
>
> **概要:**
> このコードは、REST APIを利用して他のWordPressサイトから記事を取得し、リスト形式で表示します。子テーマの `functions.php` に追加し、投稿や固定ページにショートコード `[remote_posts]` を使用することで簡単に表示できます。
>
> **特徴:**
> - REST APIから取得したデータをデコードし、HTML形式で整形します。
> - 最初の10件の投稿にはアイキャッチ画像を表示します。
> - 各記事には奇数・偶数で異なるCSSクラスを自動付与します。
> - 簡単にカスタマイズ可能で、カテゴリや記事IDを指定するなどのフィルタリングが可能です。
>
> **使用方法:**
> 1. このコードを `functions.php` に追加します。
> 2. 投稿や固定ページに `[remote_posts]` を記載して記事を表示します。
>
> **オプション例:**
> REST APIのURLをカスタマイズすることで、さまざまなフィルタリングが可能です:
> - **カテゴリ指定**
> 特定のカテゴリの記事のみを取得する:
> `https://mydomain.com/wp-json/wp/v2/posts?_embed&per_page=15&categories=123`
>
> - **記事ID指定**
> 特定の記事だけを取得する:
> `https://mydomain.com/wp-json/wp/v2/posts?_embed&include=101,102,103`
>
> - **タグ指定**
> 特定のタグの記事を取得する:
> `https://mydomain.com/wp-json/wp/v2/posts?_embed&per_page=15&tags=456`
>
> - **並び順変更**
> 記事を取得した順に並べる:
> `https://mydomain.com/wp-json/wp/v2/posts?_embed&per_page=15&orderby=include`
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment