Trusted WordPress tutorials, when you need them most.
Beginner’s Guide to WordPress
WPBカップ
25 Million+
Websites using our plugins
16+
Years of WordPress experience
3000+
WordPress tutorials
by experts

WordPressでカスタム投稿タイプごとに区切ったRSSフィードを作成する方法

編集メモ: WPBeginner のパートナーリンクから手数料を得ています。手数料は編集者の意見や評価に影響を与えません。編集プロセスについて詳しく知る。

WordPressサイトのカスタム投稿タイプごとに区切ったRSSフィードを作りたいですか?

カスタム投稿タイプにより、サイトオーナーは独自のコンテンツタイプを追加することができます。これらの投稿タイプは、RSSフィードを含む投稿やページと同様の機能を持つことができます。

この投稿では、WordPressでカスタム投稿タイプのRSSフィードを簡単に区切り作成する方法を紹介する。

Making RSS feed for custom post types in WordPress

WordPressでカスタム投稿タイプのRSSフィードを区切り作成する

初期設定では、WordPressはあなたのサイトのためにいくつかのRSSフィードを生成します。

例えば、最近のブログ投稿はすべてサイトのメインRSSフィードに表示されます。このフィードは、ドメイン名に/feed/を追加することでアクセスできます:

https://example.com/feed/

ほとんどの初心者が知らないのは、WordPressはサイトのアーカイブページごとに区切りRSSフィードを生成しているということだ。

例えば、カテゴリー、タグ、投稿者、カスタム投稿タイプのRSSフィードが区切られている。

あなたのサイトにmoviesというカスタム投稿タイプがあるとします。投稿タイプのアーカイブページにアクセスすると、その投稿タイプで作成されたすべてのコンテンツを表示できます:

https://example.com/movies

Example of a custom post type archive page

RSSフィードを表示するには、カスタム投稿タイプのアーカイブURLの次の投稿に/feed/を追加するだけです。

https://example.com/movies/feed/

Custom post type RSS feed in WordPress

または、WordPressのメインRSSフィードに投稿タイプのパラメータを追加してフィードを表示することもできます。例えば

https://example.com/feed/?post_type=movies

このURLはmoviesというカスタム投稿タイプのみを取得します。

Alternate custom post type RSS feed URL

カスタム投稿タイプRSSフィードへのリンク追加

WordPressサイトのカスタム投稿タイプのRSSフィードにアクセスする方法がわかったので、そのURLを使ってカスタム投稿タイプのフィードへのリンクを作成することができます。

例えば、訪問者が簡単に投稿を購読できるように、カスタム投稿タイプのアーカイブページにアイコンやプレーンテキストのリンクを表示したいかもしれません。

1.ブロックテーマでカスタム投稿タイプのRSSフィードへのリンクを追加する

フルサイト編集をサポートするブロックテーマを使用している場合、リンクを追加する方法は以下の通りです。

まず、WPCodeプラグインを使用して、WordPressサイトにカスタムコードスニペットを追加する必要があります。

注:このコード・スニペットを追加するために使用できる WPCodeの無料版もあります。

まず、WPCodeプラグインをインストールして有効化し、Code Snippets ” + Add New Snippetのページにアクセスします。

Add new snippet

ここから、「カスタムコードを追加(新規スニペット)」設定の下にある「スニペットを使用」ボタンをクリックします。

次の画面で、コード・スニペットの名前をプロバイダーで指定します。これは、スニペットを識別するのに役立つものであれば何でも構いません。その後、コードタイプで「PHP Snippet」を選択します。

最後に、以下のコードをコード・プレビュー・ボックスに追加する:

    if (is_post_type_archive('movies')) {
            $post_type = get_queried_object();
            // Get RSS Feed URL
            $rss_feed_url = get_post_type_archive_feed_link($post_type->name);
            // Output the shortcode content
            return '<p>Subscribe to <a href="' . esc_url($rss_feed_url) . '">' . $post_type->label . '</a></p>';
        }
    }
// Register the shortcode
add_shortcode('custom_post_type_rss_link', 'custom_post_type_rss_link_shortcode');

movies’をあなたのカスタム投稿タイプの名前に置き換えることを忘れないでください。

WPCodeプラグインのスニペットとしてどのように見えるかを示します:

Adding custom code for RSS feed link

ここで「有効化」スイッチをオンにし、スニペットを「保存/更新」する。

このコードは、ユーザーが特定のカスタム投稿タイプのアーカイブページを表示したかどうかを自動的に検出し、購読するためのリンクを表示します。

そして、リンクを表示するためにブロックテーマやウィジェットで使用できるショートコードを作成します。

次に、サイトエディターを起動するために外観 ” エディターページにアクセスする必要があります。その後、左メニューからTemplates ” Archiveオプションを選択します。

Edit Archive template in Site Editor

次に、RSSフィードのリンクを表示したい場所にShortcodeブロックを追加します。

アーカイブ名のすぐ下に表示するのが理想的です。

Add shortcode block

ショートコード・ブロックの中に、以下のショートコードを追加する必要があります:

[custom_post_type_rss_link]。

その後、変更を保存することを忘れないでください。

カスタム投稿タイプのアーカイブページにアクセスして、RSSフィードのリンクを表示できるようになりました。

RSS feed link preview

2.クラシックテーマでカスタム投稿タイプのRSSフィードへのリンクを追加する

これを行う最も簡単な方法は、WordPressテーマにカスタム投稿タイプ用の区切りテンプレートを作成することです。

FTPクライアントを使用してWordPressサイトに接続し、/wp-content/themes/your-current-theme/フォルダに移動します。

カスタム投稿タイプがmoviesの場合、WordPressテーマフォルダーにarchive-{post_type}.phpファイルを作成します。

その後、テーマのarchive.phpテンプレートからコンテンツをコピーし、新しいテンプレートのカスタマイザーを開始するだけです。

投稿タイプのアーカイブフィードには、以下のコードを使ってプレーンなHTMLリンクを追加するだけです:

<p><strong>Subscribe to: <a href="https://example.com/movies/feed/">Movies</a></strong></p>

URLを投稿タイプのフィードURLに変更することをお忘れなく。

さて、このコードタイプの問題点は、その特定の投稿タイプのためだけに新しいテンプレートファイルを作成しなければならないことです。

次の投稿タイプでは、すべてのアーカイブページの投稿タイプRSSフィードリンクを動的に生成することができます。

以下のコードをテーマのarchive.phpテンプレートファイルに追加するだけです。

<?php if ( is_post_type_archive() ) { 
$post_type = get_post_type( get_queried_object_id() );?> 
				<p><strong>Subscribe to: <a href="<?php echo get_post_type_archive_link( $post_type  ); ?>feed/"><?php post_type_archive_title(); ?></a></strong></p>
<?php } ?>		

このコードでは、投稿タイプのアーカイブページタイトルの下にリンクを追加するだけで、ユーザーにこの特定のコンテンツタイプの購読を促します。

ボーナスのヒントカスタム投稿タイプをメインRSSフィードに追加する

カスタマイザー投稿タイプのRSSフィードは、フィード読者によって発見されにくいため、ほとんどのユーザーはサイトのRSSフィードをより簡単に見つけることができます。

つまり、メインのRSSフィードを購読しているユーザーは、カスタム投稿タイプで公開するコンテンツを見逃すことになります。

サイトのメインRSSフィードに表示されるカスタム投稿タイプのコンテンツを追加することで、この問題を簡単に解決できます。

そのためには、WordPressブログにカスタム・コード・スニペットを追加する必要があります。WordPressでカスタムコードスニペットを追加するには、WPCodeを使用することをお勧めします。

まず、無料のWPCodeプラグインをインストールし、有効化する必要があります。詳しくは、WordPressプラグインのインストール方法の投稿をご覧ください。

プラグインを有効化したら、WordPressの管理サイドバーからCode Snippets ” + Add Snippetのページにアクセスします。

ここから、「カスタムコードを追加する(新規スニペット)」設定の下にある「スニペットを使用する」ボタンをクリックする必要があります。

Add new snippet

カスタム・スニペットの作成」ページが表示されますので、コード・スニペットの名前を入力してください。

この名前はどこにも表示されず、識別のためだけに使用される。

次に、右側の「コードタイプ」ドロップダウンメニューから「PHPスニペット」オプションを選択します。

Snippet title and code type

その後、カスタム・コード・スニペットを追加する準備ができました。

以下のコードをコピー&ペーストして、コードプレビューボックスに貼り付けるだけです:

function myfeed_request($qv) {
    if (isset($qv['feed']) && !isset($qv['post_type']))
        $qv['post_type'] = array('post', 'movies', 'books');
    return $qv;
}
add_filter('request', 'myfeed_request');

コードを追加したら、'[‘post_type’]’と書かれている場所の次の投稿タイプにカスタム投稿タイプの名前を入力します。カテゴリー:’post’、’movies’、’books’です。

このカスタム投稿タイプはメインのWordPress RSSフィードに追加されます。

Add code snippet

次に、ページの一番上までスクロールして戻り、「Inactive」スイッチを「Active」に切り替える必要がある。

最後に、「Save Snippet」ボタンをクリックし、WordPressサイトにコードを保存して実行することをお忘れなく。

Save snippet

以上で、カスタム投稿タイプのコンテンツがサイトのメインRSSフィードに追加されます。

WordPressでカスタム投稿タイプのRSSフィードを区切り作成する方法について、この投稿がお役に立てれば幸いです。WordPressのRSSフィードの最適化に関するチュートリアルや、WordPressのRSSプラグインのエキスパートによるベストピックもご覧ください。

If you liked this article, then please subscribe to our YouTube Channel for WordPress video tutorials. You can also find us on Twitter and Facebook.

情報開示 私たちのコンテンツは読者支援型です。これは、あなたが私たちのリンクの一部をクリックした場合、私たちはコミッションを得ることができることを意味します。 WPBeginnerの資金源 をご覧ください。3$編集プロセスをご覧ください。

Avatar

Editorial Staff at WPBeginner is a team of WordPress experts led by Syed Balkhi with over 16 years of experience in WordPress, Web Hosting, eCommerce, SEO, and Marketing. Started in 2009, WPBeginner is now the largest free WordPress resource site in the industry and is often referred to as the Wikipedia for WordPress.

究極のWordPressツールキット

ツールキットへの無料アクセス - すべてのプロフェッショナルが持つべきWordPress関連製品とリソースのコレクション!

Reader Interactions

30件のコメント返信を残す

  1. Syed Balkhi says

    Hey WPBeginner readers,
    Did you know you can win exciting prizes by commenting on WPBeginner?
    Every month, our top blog commenters will win HUGE rewards, including premium WordPress plugin licenses and cash prizes.
    You can get more details about the contest from here.
    Start sharing your thoughts below to stand a chance to win!

  2. Buddika Wijerathena says

    Is there way to create RSS feed for single page ?
    Ex – example.com/single-post-type/single-post-name

    How to create RSS for this ?

    • WPBeginner Support says

      RSS feeds are for listing multiple posts, the RSS would include a link to the post itself. We would not recommend creating a specific RSS feed for one post or page.

      管理者

  3. Milan says

    Hey, and do you know any rss wordpress plugin for visitors ? As big new companies have own RSS creator to put rss on some freelancer sites, I want to it same, but not with post but with custom post types….do you know some RSS plugin ?

  4. Tori says

    It still doesn’t work for me – I’m trying to display an RSS Feed for the content on this page –

    and displays the RSS feed for all of my posts. Any thoughts?

      • Tori says

        Thank you for the response. I’m sorry for the confusion as it looks like my links didn’t appear. The posts that appear on that page are custom posts (it’s like the blog roll but for custom posts), but the method to do the RSS feed didn’t work for me. Any thoughts or suggestions?

        • WPBeginner Support says

          Hey Tori,

          If you are certain that the posts belong to a custom post type, then you need to find out the name used by the custom post type. You can do that by clicking on the custom post type menu item in your WordPress admin sidebar. Clicking on it will take you to an admin page listing all the posts in that post type. Now if you look into the browser address bar you will see something like this:

          http://www.example.com/wp-admin/edit.php?post_type=your_post_type

          The part that appears after the post_type= is your custom post type name. Now you need to use this to reach the RSS feed URL for that custom post type.

          http://www.example.com/feed/?post_type=your_post_type

          Hope this helps.

    • Tori says

      Thank you for the response. When I do that, I see this error message — any thoughts?

      ______

      This page contains the following errors:

      error on line 2 at column 1: Extra content at the end of the document
      Below is a rendering of the page up to the first error.

  5. Cédric Charles says

    Hi and thanks for this !

    I would like to add custom fields for my custom post type feed (not for the regular posts, only for my custom post type).

    How could I do that ?

    Thanks a lot !

  6. mike says

    I’ve gotten this to work but it limits the posts in the rss feed to 10 when I feedburn it. Is there any way to create a full rss feed for custom post types without limiting the quantity of posts it pulls in?

  7. Andrew says

    Hi there. Could you tell me how to create a feed for all post types, so someone can signup to a single RSS feed for all posts on the site, regardless of which post type they are in?

  8. Rems says

    THANK YOU VERY MUCH, i was looking for this info for 2 hours. Glad i found your info. Fu…. taxonomy, where were you?!! aahhh!
    Work so fine now

  9. AlexAmelines says

    it works for me and I love you for it. I created a link to each pos type I’ve got, but in the RSS reader they are all called the same, any way I can affect the title of the feed to reflect the post type?

  10. Alan says

    If I am using a slug re-write for the taxonomy that wouldn’t make a difference and be causing the problem would it? I’ve tried both ways and it doesn’t work either way, with the actual registered taxonomy title or the slug.

  11. Aldi says

    I can’t seem to make that work. It only takes me back to the custom post type archive page again.

    Btw, I use Custom Post Permalinks plugin to allow custom post type permalinks and archive pages. Could that have created the issue??

    Cheers!

    • Aldi says

      Oops, sorry.. it was my theme’s problem. I had an action call to redirect anything that is related to custom post types, thus the redirection of the feeds. But got it fixed now.

返信を残す

コメントありがとうございます。すべてのコメントは私たちのコメントポリシーに従ってモデレートされ、あなたのメールアドレスが公開されることはありませんのでご留意ください。名前欄にキーワードを使用しないでください。個人的で有意義な会話をしましょう。