/
CATEGORY
WordPress
/
讓 WordPress 的 RSS Feed 也能讀到自訂的文章類型 (post type)

讓 WordPress 的 RSS Feed 也能讀到自訂的文章類型 (post type)

MUKI AI Summary

WordPress 預設的 RSS Feed 只能讀取 post type 為 post 的文章。若想讓 RSS Feed 讀取自訂文章類型,可在 functions.php 加入語法,顯示所有自訂文章類型。

若只希望顯示特定文章類型,需修改語法,設定 post type 為 post、course 和 movies。這樣即可在 RSS Feed 中讀取指定的自訂文章類型。...

如果你有使用 WordPress 的自訂文章類型 (custom post type),應該會發現預設的 RSS Feed 是讀不到該內容的,那是因為 WordPress 的 RSS,預設只會讀到「post type = post」的文章,也就是我們後台系統的「文章」。

讓 RSS Feed 讀到自訂文章類型

因此我們可以在 functions.php 加入一些語法,就可以讓 RSS Feed 讀到自訂文章類型:

<?php
function myfeed_request($qv) {
  if (isset($qv['feed']))
    $qv['post_type'] = get_post_types();
  return $qv;
}
add_filter('request', 'myfeed_request');
?>

以上這段語法會顯示所有的自訂文章類型。

指定要讀的文章類型

但如果你不希望顯示所有文章類型的話,只要將語法稍作修改即可:

<?php
function myfeed_request($qv) {
  if (isset($qv['feed']) && !isset($qv['post_type']))
    // 修改要放到 RSS Feed 的 post type。
	$qv['post_type'] = array('post', 'course', 'movies');
  return $qv;
}
add_filter('request', 'myfeed_request');
?>

參考資料

How to Add Custom Post Types to Your Main WordPress RSS Feed

How to Add Custom Post Types to Your Main WordPress RSS Feed

Learn how you can add Custom Post Types to your main WordPress RSS feeds by including or excluding specific custom post types.

https://www.wpbeginner.com/wp-tutorials/how-to-add-custom-post-types-to-your-main-wordpress-rss-feed/

歡迎給我點鼓勵,讓我知道你來過 :)

MUKI says:

如果文章有幫助到你,歡迎分享給更多人知道。文章內容皆為 MUKI 本人的原創文章,我會經常更新文章以及修正錯誤內容,因此轉載時建議保留原出處,避免出現版本不一致或已過時的資訊。

本文地址:https://muki.tw/add-custom-post-type-to-wordpress-rss-feed/ 已複製

Subscribe
Notify of
guest

0 則留言
Oldest
Newest Most Voted
Inline Feedbacks
View all comments
Copyright © since 2008 MUKI space* / omegaSS theme All Rights Reserved.