WordPressには、ウェブサイトのコンテンツを自動的に共有するRSSフィードがデフォルトで用意されており、購読者は簡単に最新情報を入手することができます。
しかし、ブログ記事、製品アップデート、イベント告知など、特定の種類のコンテンツだけを共有したい場合は、カスタムRSSフィードを設定することができます。こうすることで、オーディエンスの興味に合ったコンテンツを提供することができます。
これにより、購読者は自分にとってより有意義な最新情報を受け取ることになり、エンゲージメントが向上します。さらに、コンテンツをどこでどのように共有するかをよりコントロールできるようになります。
この投稿では、WordPressでカスタムRSSフィードを簡単に作成する方法をご紹介します。
なぜWordPressでカスタムRSSフィードを作成するのか?
WordPressにはRSSフィードがビルトインされており、カスタムコンテンツや サムネイルを 簡単に追加することができます。
しかし、WordPressウェブサイトに特定のタイプのコンテンツやカスタム投稿タイプがある場合は、そのためのカスタムRSSフィードを作成することができます。
例えば、特定の著者による独占コンテンツを購読者に配信する場合、カスタマイザーRSSフィードを作成することで、購読者にコンテンツの情報を提供することができます。これにより、検索エンジンのランキングを向上させることができます。
さらに、カスタマイザーフィードでは、著者名や出版日などの関連情報を有効化することで、より自由度の高い情報をユーザーに提供することができます。
それでは、WordPressでカスタムRSSフィードを簡単に作成する方法を順を追って見ていきましょう。
注:稼働中のサイトに大きな変更を加える前に、WordPressサイトの完全なバックアップを作成する必要があります。
WordPressでカスタムRSSフィードを作成する方法
WordPressのカスタムRSSフィードは、テーマのfunctions.phpファイルにカスタムコードを追加することで簡単に作成できます。
このフィードは、WordPressサイトの投稿のタイトル、リンク、公開日、投稿者、抜粋を表示します。
しかし、このコードを入力する際のわずかなエラーが、あなたのサイトを壊し、アクセス不能にする可能性があることに留意してください。
そのため、WPCodeの使用をお勧めします。市場で最高のWordPressコードスニペットプラグインであり、ウェブサイトにカスタムコードを安全かつ簡単に追加することができます。
まず、WPCodeプラグインをインストールし、有効化する必要があります。詳しい手順については、WordPressプラグインのインストール方法についての初心者ガイドをご覧ください。
注:このチュートリアルでは、無料のWPCodeプラグインを使用することができます。しかし、有料プランにアップグレードすると、テストモード、クラウドライブラリ、条件ロジックなど、より多くの機能がアンロックされます。
有効化したら、WordPressの管理サイドバーからコードスニペット ” + スニペットの追加ページにアクセスします。ここで、「カスタムコードを追加(新規スニペット)」設定の下にある「スニペットを使用」ボタンをクリックします、
カスタムスニペットの作成」ページが表示されますので、まずはコードスニペット名を入力してください。
次に、右側のコードタイプのドロップダウンメニューから「PHP Snippet」オプションを選択します。
次に、以下のカスタムコードを「コードプレビュー」ボックスに追加します:
add_action('init', 'customRSS');
function customRSS(){
add_feed('Feed', 'customRSSFunc');
}function customRSSFunc(){
get_template_part('rss', 'feedname');
/**
* Template Name: Custom RSS Template - Feedname
*/
$postCount = 5; // The number of posts to show in the feed
$posts = query_posts('showposts=' . $postCount);
header('Content-Type: '.feed_content_type('rss-http').'; charset='.get_option('blog_charset'), true);
echo '<?xml version="1.0" encoding="'.get_option('blog_charset').'"?'.'>';
?>
<rss version="2.0"
xmlns:content="http://purl.org/rss/1.0/modules/content/"
xmlns:wfw="http://wellformedweb.org/CommentAPI/"
xmlns:dc="http://purl.org/dc/elements/1.1/"
xmlns:atom="http://www.w3.org/2005/Atom"
xmlns:sy="http://purl.org/rss/1.0/modules/syndication/"
xmlns:slash="http://purl.org/rss/1.0/modules/slash/"
<?php do_action('rss2_ns'); ?>>
<channel>
<title><?php bloginfo_rss('name'); ?> - Feed</title>
<atom:link href="<?php self_link(); ?>" rel="self" type="application/rss+xml" />
<link><?php bloginfo_rss('url') ?></link>
<description><?php bloginfo_rss('description') ?></description>
<lastBuildDate><?php echo mysql2date('D, d M Y H:i:s +0000', get_lastpostmodified('GMT'), false); ?></lastBuildDate>
<language><?php echo get_option('rss_language'); ?></language>
<sy:updatePeriod><?php echo apply_filters( 'rss_update_period', 'hourly' ); ?></sy:updatePeriod>
<sy:updateFrequency><?php echo apply_filters( 'rss_update_frequency', '1' ); ?></sy:updateFrequency>
<?php do_action('rss2_head'); ?>
<?php while(have_posts()) : the_post(); ?>
<item>
<title><?php the_title_rss(); ?></title>
<link><?php the_permalink_rss(); ?></link>
<pubDate><?php echo mysql2date('D, d M Y H:i:s +0000', get_post_time('Y-m-d H:i:s', true), false); ?></pubDate>
<dc:creator><?php the_author(); ?></dc:creator>
<guid isPermaLink="false"><?php the_guid(); ?></guid>
<description><![CDATA[<?php the_excerpt_rss() ?>]]></description>
<content:encoded><![CDATA[<?php the_excerpt_rss() ?>]]></content:encoded>
<?php rss_enclosure(); ?>
<?php do_action('rss2_item'); ?>
</item>
<?php endwhile; ?>
</channel>
</rss>
コードのadd_feed
行の後にFeed
テキストを置き換えることで、カスタムRSSフィードの名前を好きなものに変更できるようになりました。
フィードを作成したら、RSSフィードのURLにアクセスして表示することができます。以下のように表示されます:yourdomain.com/feed/フィード名。
次に、挿入セクションまでスクロールダウンし、「自動挿入」モードを選択する。
カスタムRSSフィードはコード有効化時に自動的に作成されます。
最後に、一番上までスクロールして戻り、「非アクティブ」スイッチを「アクティブ」に切り替える。
その後、「Save Snippet」ボタンをクリックして設定を保存します。
それができたら、WordPressダッシュボードから 設定 ” パーマリンクのページにアクセスします。
ここで、「変更を保存」ボタンをクリックするだけで、リライトルールがフラッシュされ、サイトの新しいパーマリンクが生成されます。
これで、yourdomain.com/feed/feednameのURLにアクセスすることで、カスタマイザーRSSフィードを表示できるようになります。
カスタムRSSフィードの例です:
ボーナス:カスタムRSSフィードのトラブルシューティング
カスタムRSSフィードを作成する際に発生する可能性のあるエラーと、その解決方法について説明します。
フィードを表示しようとすると404エラーが出ます!
カスタムフィードを表示しようとしたときに404エラーが表示される場合は、URLのフィード名が正しいかどうかを確認してください。
これは、add_feed
関数で指定したものでなければなりません。フィード名が正しい場合は、リライトルールが正しくフラッシュされていない可能性があります。パーマリンクを保存し直すことで簡単にできます。
パーマリンクを保存し直しても問題が解決しない場合は、WPCodeスニペットのadd_ feed
関数の隣に次のコードを追加することで、強制的に書き換えフラッシュを行うことができます:
global $wp_rewrite;
$wp_rewrite->flush_rules();
私のフィードは正しいとは言えない!
W3Cはフィード検証サービスを提供しており、生成されたフィードを検証し、フィードが検証されていない場所について具体的な詳細を提供することができます。
そうすれば、コード・スニペットを簡単に編集して、これらの問題を解決することができる。
<language />の正しい表記がエラーになります!
これは、WordPressインストール時にRSS言語が設定されていない場合によく起こります。
これを行うには、WPCodeに以下のコードを追加して言語オプションを更新します:
function rssLanguage(){
update_option('rss_language', 'en');
}
add_action('admin_init', 'rssLanguage');
update_option
関数の第2引数を編集して、必要な言語に変更してください。RSS言語コードの全リストをチェックする。
スニペットを保存するとエラーはなくなり、カスタマイ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.
Av
thanks for this informative article
can i use this method to create an rss feed for woocommerce products?
(it’s a deal aff site based on woo)
WPBeginner Support
Unless we hear otherwise you should be able to
管理者
Jonathan
Following this tutorial, for some reason changing the $postCount does not work.
Eg,
$postCount = 20; // The number of posts to show in the feed
Only by changing the default number inside of Wordpress Reading settings does it work. Very weird. How do we overwrite the wordpress settings, or stop the wordpress settings from overriding our code?
WPBeginner Support
Your specific theme may be overriding the number of posts to show, we would first recommend checking with the support for your specific theme and they should be able to assist.
管理者
Shahid
Hi.
How i can add custom post types and custom fields in feed?
WPBeginner Support
You would want to take a look at two of our other articles below for that information.
https://www.wpbeginner.com/wp-tutorials/how-to-add-custom-post-types-to-your-main-wordpress-rss-feed/
https://www.wpbeginner.com/wp-tutorials/wordpress-custom-fields-101-tips-tricks-and-hacks/
管理者
Dexter Nelson
Is there any way to make this permanent? I keep having to repeat this step every few weeks when the theme is updated.
WPBeginner Support
For that, you would want to create a child theme following our guide below:
https://www.wpbeginner.com/wp-themes/how-to-create-a-wordpress-child-theme-video/
管理者
Dexter Nelson
Thanks. But doesn’t that mean when the theme is updated I’ll have to update the child theme? If that’s the case and I have to put in the work either way, I might as well put in the work to create and maintain a plugin and help out others instead of just myself, right?
WPBeginner Support
Child themes should not be affected by updates to the parent theme but if you have the ability to create a plugin then you can certainly go down that path to help others as well.
Thommy Kusbin
is there any plugin to do this kind of custom rss feed XML? I want to use my own tag xml.
WPBeginner Support
We do not have a recommended plugin at this time, we will be sure to keep an eye out.
管理者
MOOSA
How can I use “wp_get_shortlink()” Instead of “the_permalink_rss()”
John Snyder
I need to create an XML feed for a specific page on my Wordpress website. Here is the page will I have to edit my functions.php file or is there an easier way?
Alok Shrestha
Hello,
This article is very helpful but I got some issue.
I did exactly like in your code. But it gives me an error as
This page contains the following errors:
error on line 1 at column 7: XML declaration allowed only at the start of the document
Below is a rendering of the page up to the first error.
Could you please help me out what could possibly go wrong here?
This is very important for me.
Thank you.
Nathan
I got the same error. Did you figure out the fix?
Rich
Use ob_clean(); after php tag
Guillermo
Hi, is it possible to change de order of the posts in my feeds without creating a new custom feed? I want to use the current feed files but just want to change the displayed order of theme. Using PHP queries as an example, changing order from DESC to RAND()
I will appreciate you help. Thanks.
Jeremy
Great post! I definitely fall under the rookie status. Many feed readers/aggregators don’t seems to like the /feed/ URL structure, even through the xml returned validates. Is there a way to append the url to end with rss.xml?
Dave
Did you get this to work with .xml at the end?
JDURAN
Sorry to say, however, implementing the codes for custom rss has effectively wiped out my entire blog. Having to do a restore and it is taking a long time to get my blog back up. Any advise?
Mark
You need to connect to your site via FTP or your hosting’s file browser in cPanel, and simply remove what you added. If you added the code as a custom plugin, then just rename the plugin folder. If you added the code in functions.php, edit the file and remove the code. Don’t forget to backup the file before editing. Hope this helps for the next time.
jim
Fantastic! This works as advertised!
DrLightman
Never mind, it does. I had a bug with YOAST SEO that with the /category/ prefix removal. I have warned them of it I hope they will fix it.
DrLightman
Hello, thank you for the article, but it seems this will only work for the main site feed, not for the specific catgories feeds:
mysite.com/category/mycat/feed/feedname/
Manuel
Doesn’t work anymore.
I got a 404 error, so I activated debug mode. Debug mode says “Notice: The Called Constructor Method For WP_Widget Is Deprecated Since Version 4.3.0! Use __construct()”
Manuel
Ok, I think the problem was elsewhere and this notice is from a different plugin.
git
unfortunately this is restricted in its usefulness. inserting screenshots and sample pages would be very helpful, particularly for amateurs/rookies like me.
Tiffany
Great article. Any way to add the featured image for the post to the feed?
WPBeginner Support
Please see our guide on how to add featured image to your WordPress RSS feed.
管理者
CT
Please see below screenshots – what determines the “…” or “[…]” or “Read More ->” etc etc in feeds?
I’m using the default /feed in 2 different WP sites & these 2 screenshots obviously are showing something different at the end of their excerpts. Thanks!
CT
Apologizes but that code did not come through clear lol. Lets try that again. Go here to see the exact, raw code I’m looking to remove:
WPBeginner Support
This code is added by Yoast SEO plugin to RSS feeds. You can turn it off by visting SEO » Advanced page.
管理者
Stef
Hey, I want to exclude certain post_formats from my custom rss feed. Is that possible and if so, how?
Neha
Hello,
When I try to view the feed I got 404 Page not found error. I am trying to add code:
global $wp_rewrite;
$wp_rewrite->flush_rules();
then I also got same error. Could this be why?
Jordan
How do you add multiple feeds? Array is not working. Thanks.
Jon Harvey
How would you add multiple custom feeds? Couldn’t get an array to work.
Thanks
Jon Harvey
Got it to work neatly thanks to birgire at Stack exchange:
add_action( ‘init’, ‘custom_feeds’ );
function custom_feeds()
{
foreach( array( ‘feedname1’, ‘feedname2’ ) as $name )
{
add_feed( $name,
function() use ( $name )
{
get_template_part( ‘rss’, $name );
}
);
}
}
Neal Pope
The “follow” button shows up at the bottom right corner when viewing posts on a personal computer, but does not appear when viewing on a mobile devise (smartphone) (unless I’m just missing it.
Limbani
Thanks for sharing it’s work perfectly…
Jeff
Sorry, but everything about this is misleading. this is not “beginner” work. and it is not worded for beginners. This is totally irresponsible and can cause major issues to people’s websites.
WPBeginner Support
We have updated the article to add a note about this.
管理者
Christine
I got this working (kind of). The feed page exists, but will not validate and says there is an error on line 1.
I copied the code exactly from this page and haven’t changed anything. What could be going wrong here?
Flávia
W3 will not validate mine as well and says there is an error on line 17.
And I am getting the 404 page!
What should I do?
Eugene Asiamah
Hello,
Please i need help in configuring my rss to show all my post content on not just an excerpt of it.
Thank you.
Georgi
Hello,
I need to show only date without time for pubDate. When I use
It’s doesn’t work for firefox,IE.
Can you help me?
Thank you in advance!
Jan-Philipp
Hi.
I would like to change the update pattern of the feed with
sy:updatePeriod
echo apply_filters( ‘rss_update_period’, ‘weekly’ );
/sy:updatePeriod
sy:updateFrequency
echo apply_filters( ‘rss_update_frequency’, ‘1’ );
/sy:updateFrequency>
sy:updateBase
2015-03-29T01:00:00+09:00
/sy:updateBase
But whatever I tried, it automatically adds a new post when I publish it and does not wait till Sunday ( as defined in the code above) to add it to the RSS.
Do you have any suggestions? Do I maybe need to find a solution within the WP Query?
Your help would be much appriciated.
Jan-Philipp
Hi.
I would like to change the update pattern of the feed with
2015-03-29T01:00:00+09:00
But whatever I tried, it automatically adds a new post when I publish it and does not wait till Sunday ( as defined in the code above) to add it to the RSS.
Do you have any suggestions? Do I maybe need to find a solution within the WP Query?
Your help would be much appriciated.
Issabellla
Hi.I try to do as your mention but I’ve seen my site cannot access to the feed page.This is My site I try to use /feed and a lot of word which is mention in wordpress.com or wordpress.org even in the google.Please help me.
WPBeginner Support
Your site’s feed looks fine to us.
管理者
xuamox
What determines that the feed will be displayed at feed/feedname? I have tried to follow the tutorial, but not luck at all. The feed is not publishing at feed/feedname.php
Wouter Bertels
Following these exact steps I got this error:
Fatal error: Cannot redeclare get_bloginfo_rss() (previously
declared in
/home/public_html/wp-includes/feed.php:25)
in /home/public_html/wp-content/themes/xxx/rss-name.php on line 39
George
Hi,
My feed is OK when this code is added:
global $wp_rewrite;
$wp_rewrite->flush_rules();
and returns a 404 when it is removed.
I only remove it as per your instructions, which is to remove it after reloading wordpress.
At this moment I am testing it on my local server. Could this be why?
ybmgryzzz
Hi there. Thanks for this post! Really appreciated. Worked well at first go!
I would also like some assistance as Brian below. For the full text in the RSS instead of the post excerpt.
Thanks in advance.
Brian
I am trying to add a second RSS feed that is full text (my first RSS feed is summary). If I wanted this custom RSS feed to be full text instead of summary excerpt, what would I change?
Thanks so much!
Steve Marks
Thanks for this. Just what I was looking for!
I had an issue when I used add_feed(). I could view the RSS feed in the browser absolutely fine, but it would return a 404 when I entered the URL into a third party application (ie. MailChimp, W3C feed validator etc). I had tried flushing the permalinks etc.
The only way I could get round the issue was to create a new blank page and select the template as the one you’ve outlined in this article.
Not sure why it didn’t work, but hopefully this offers a viable solution for others in this situation.
Thanks again!
Josh McClanahan
Your article is right on the path of what I was looking for. One question I have (as a noob to RSS), how can I add a link to an RSS feed?
The site that I need this for is a church and would like to have a custom feed that would make it easier for our members to be alerted to newly posted sermon posts (that have mp3 download links).
Currently, the feed shows the text for the links that I have created on the posts for the sermons. But it only acts as text. Any help would be appreciated.
Josh McClanahan
Just noticed that I didn’t ask my question correctly.
What I was trying to ask was, how can I display a link in the RSS Feed? The links that are in the content of my posts are broken in the feed.
Thanks for your help and this article.
Josh McClanahan
Still curious, if anyone knows why my links would be broken and only show as plain text in a feed?
To see what I mean checkout: http://amfmchurch.com/feed
You will see “Download” in plain text. I would like to make it so that people can click the anchor link and download the mp3. This works find on the actual site.
Thanks for any help.
Neil
Hi, I have the default rss feed.. but I would like to be able to either reduce the size of the images from what is displayed in the post…. (to a maximujm width of 265px) OR to just use the post thumbnail in the rss.
Is either of the above possible?
The reason I am looking to do this is that my RSS feeds an APP and the APP will only display images up to 265px in width (before horizontal scrolling) and 265px is very limiting for online/pc blog posts…
Cathy Finn-Derecki
Thank you for this! I am making a custom RSS location part of a plugin I’m developing. As a result, I have included the text for the custom RSS in the function, not in a theme template. It’s working. However, it does not seem to work when I want to restrict the feed to a category. It gives all posts regardless. Any thoughts?
WPBeginner Support
try changing
1-click Use in WordPress
To:
1-click Use in WordPress
Replace category_name with your category name.
管理者
Francisco Espinoza
Hi Guys. How can I use this to exclude three categories from my new custom feed? What do you suggest?
Alexis
Hello,
Thank you, these explanations are really useful. I just used it to customize my feed so I could put an image in mailchimp’s “RSS to email” feature.
Thank you very much !
the Off
Hello,
Till yesterday afternoon, my Wordpress (version 3.6.1) blog http://theoff.info/wordpress/ had no problem. And I successfully added new plugins.
In the night, problems started: (1) I could not login. (2) the RSS feed http://theoff.info/wordpress/?feed=rss2 got corrupted. The feed failed validation.
I searched the Web and Wordpress Support Forum for solutions. I tried the following:
(i) successfully reset the password
(ii) deleted new plugins from ftp
(iii) renamed Theme folder by adding “-old” and plugin folder by adding “-hold” from ftp
Then I could login and up grade to 3.7.1 version and make changes to the blog.
However, login is still an issue. Every time I am following method mentioned in step (iii) to login.
RSS still does not validate.
Could you help in resolving both login and RSS feed problems?
Note: My webhost does not offer technical help. I am not an IT professional i.e. I have very little technical knowledge.
Thanks
WPBeginner Support
It is difficult to figure out. Here is one thing you can try.
1. Using FTP download your plugins to your computer for backup and then delete your plugins directory.
2. Create a new plugins directory.
3. Install and all your plugins one by one by downloading fresh copy of the plugin from the source. After you activate each plugin log out and and log back in to see if the problem occurs again. You will probably find a plugin that may be causing the problem or you wont. This could mean that a plugin file on your site may have been compromised.
If this process does not help you, repeat this process process with your themes as well. If you have made a lot of changes to your existing themes then you might want to back it up on your computer by downloading theme folder through FTP. Then delete themes folder from your webserver and create a new themes directory in wp-content folder on your server. From WordPress admin area download and install a fresh copy of default twenty thirteen theme.
Let us know what happens when you do all that. Make Sure to Backup your Site first.
管理者
Bertrand
Hello,
When I try to open the new feed, I get an error message:
Fatal error: Call to undefined function query_posts() in /xxxx/wp-content/themes/xxx/rss-feedname.php on line 6
I tried on localhost and web hosting, it is the same…
How can I fix it?
Thanks
Bertrand
My fault: http://wordpress.org/support/topic/error-message-call-to-undefined-function-query_posts?replies=5
ADv
Does the php template need close tag ?> in the end?
ADv
No, it doesn’t. Silly me = )