倡萌之前已經(jīng)介紹了 WordPress的RSS Feed地址是什么?如何添加?如何訂閱?,今天補(bǔ)充一下 WordPress RSS Feed 設(shè)置及優(yōu)化技巧。
RSS Feed 基本設(shè)置
在后臺(tái) > 設(shè)置 >閱讀,可以設(shè)置 Feed 輸出的篇數(shù)和類型:

注:如無特殊說明,下面的代碼都添加到當(dāng)前主題的 functions.php 文件即可
Feed 輸出自定義內(nèi)容
在feed中輸出自定義內(nèi)容可以通過 ‘the_content’ 這個(gè) filter 鉤子輕松實(shí)現(xiàn),我們要做的就是使用 is_feed() 這個(gè)條件標(biāo)簽來判斷只在 Feed 輸出內(nèi)容。例如下面的例子:
function custom_rss_feed_content($content) { //定義新函數(shù)
if(is_feed()) { //只在Feed中執(zhí)行
$output = '歡迎訪問 http://www.ydqwiac.cn'; //添加自定義內(nèi)容
$content = $content . $output ; //重新設(shè)定文章內(nèi)容 $content
}
return $content; //返回最后的文章內(nèi)容
}
add_filter('the_content','custom_rss_feed_content'); //通過鉤子掛載該函數(shù)
注:
1. 代碼中的 $content 是WordPress預(yù)留的 文章內(nèi)容變量,$output 是我們自定義的變量,用來添加自定義內(nèi)容;
2. $content . $output 表示在文章原文的后面添加 $output 的內(nèi)容,如果你想在原文前面添加,可以改為 $output . $content
3. $output 后面的自定義內(nèi)容可以是 HTML 代碼,比如下面的例子:
//Feed輸出版權(quán)信息
function wpdaxue_feed_copyright($content) {
if(is_feed()) {
$post_title = get_the_title(); //獲取原文標(biāo)題
$post_link = get_permalink($post->ID); //獲取原文鏈接
$output = '<p><span style="font-weight:bold;text-shadow:0 1px 0 #ddd;">聲明:</span> 本文采用 <a rel="nofollow" title="署名-非商業(yè)性使用-相同方式共享">BY-NC-SA</a> 協(xié)議進(jìn)行授權(quán) | <a href="'.home_url().'">'.get_bloginfo('name').'</a><br />轉(zhuǎn)載請(qǐng)注明轉(zhuǎn)自《<a rel="bookmark" title="' . $post_title . '" href="' . $post_link . '">' . $post_title . '</a>》</p>';
$content = $content . $output ;
}
return $content;
}
add_filter ('the_content', 'wpdaxue_feed_copyright');
Feed 輸出自定義字段
如果你在文章中使用了自定義字段,要在Feed中輸出的話,可以使用 get_post_meta() 函數(shù)獲取自定義字段的值。假設(shè)你要調(diào)用的是 copyright 這個(gè)自定義字段,可以使用下面的代碼:
//Feed 輸出自定義字段
function fields_in_feed($content) {
if(is_feed()) {
$post_id = get_the_ID(); //獲取文章ID
$output = get_post_meta($post_id, 'copyright', true) ; // 獲取字段 copyright 的值
$content = $content.$output;
}
return $content;
}
add_filter('the_content','fields_in_feed');
Feed 輸出文章特色圖像
//Feed 輸出文章特色圖像(縮略圖)
function rss_post_thumbnail($content) {
global $post; //查詢?nèi)治恼? if(has_post_thumbnail($post->ID)) { //如果有特色圖像
$output = get_the_post_thumbnail($post->ID) ; //獲取縮略圖
$content = $output . $content ;
}
return $content;
}
add_filter('the_excerpt_rss', 'rss_post_thumbnail');
add_filter('the_content_feed', 'rss_post_thumbnail');
Feed 只輸出簡(jiǎn)碼內(nèi)容
//Feed 只輸出簡(jiǎn)碼(shortcode)內(nèi)容
function rssonly_content( $atts, $content = null) {
if (!is_feed()) return "";//如果不是Feed,不返回內(nèi)容
return $content;
}
add_shortcode('rssonly', 'rssonly_content'); //注冊(cè)簡(jiǎn)碼 rssonly
在寫文章的時(shí)候,使用簡(jiǎn)碼 [rssonly] 包含的內(nèi)容,只會(huì)在Feed輸出:
[rssonly] 非常感謝訪問 WordPress大學(xué) www.ydqwiac.cn [/rssonly]
在 Feed 中排除分類
//在Feed中排除某些分類
function exclude_cat_feed($query) {
if(is_feed()) {
$query->set('cat','-1'); //排除ID為 1 的分類
return $query;
}
}
add_filter('pre_get_posts', 'exclude_cat_feed');
如果要排除多個(gè)分類,將第 4 行修改為下面的代碼:
$query->set('cat','-1, -4, -7'); //排除ID為 1、4、7 的分類
Feed 輸出自定義文章類型的內(nèi)容
請(qǐng)移步閱讀《讓W(xué)ordPress RSS Feed輸出自定義文章類型的內(nèi)容》
禁用所有 Feed 訂閱
如果你不愿意讓別人訂閱的你網(wǎng)站,可以使用下面的代碼:
//禁用Feed訂閱
function wp_disable_feed() {
wp_die( __('抱歉,本站不支持訂閱,請(qǐng)返回<a href="'. get_bloginfo('url') .'">首頁</a>') );
}
add_action('do_feed', 'wp_disable_feed', 1);
add_action('do_feed_rdf', 'wp_disable_feed', 1);
add_action('do_feed_rss', 'wp_disable_feed', 1);
add_action('do_feed_rss2', 'wp_disable_feed', 1);
add_action('do_feed_atom', 'wp_disable_feed', 1);
//移除評(píng)論feed輸出
add_action('do_feed_rss2_comments', 'wp_disable_feed', 1);
add_action('do_feed_atom_comments', 'wp_disable_feed', 1);
//移除head中輸出的feed鏈接
remove_action( 'wp_head', 'feed_links_extra', 3 );
remove_action( 'wp_head', 'feed_links', 2 );
好了,今天就分享這些,如果你還知道其他Feed優(yōu)化技巧,歡迎和我們一起分享。
相關(guān)推薦:
3 個(gè) WordPress Feed訂閱統(tǒng)計(jì)插件
WordPress禁止采集RSS內(nèi)容的插件:Block RSS Reading





有沒有美化的代碼啊
不錯(cuò)。學(xué)習(xí)了。