7+ WordPress RSS Feed 設置及優化技巧

本文目錄1RSS Feed 基本設置2Feed 輸出自定義內容3Feed 輸出自定義字段4Feed 輸出文章特色圖像5Feed 隻輸出簡碼內容6在 Feed 中排除分類7Feed 輸出自定義文章類型的內容8禁用所有 Feed 訂閱

之前已經介紹瞭 WordPress的RSS Feed地址是什麼?如何添加?如何訂閱?,今天補充一下 WordPress RSS Feed 設置及優化技巧。

RSS Feed 基本設置

在後臺 > 設置 >閱讀,可以設置 Feed 輸出的篇數和類型:

wordpress-rss-feed-hacks-wpdaxue_com

註:如無特殊說明,下面的代碼都添加到當前主題的 functions.php 文件即可

Feed 輸出自定義內容

在feed中輸出自定義內容可以通過 ‘the_content’ 這個 filter 鉤子輕松實現,我們要做的就是使用 is_feed() 這個條件標簽來判斷隻在 Feed 輸出內容。例如下面的例子:

1
2
3
4
5
6
7
8
function custom_rss_feed_content($content) { //定義新函數
	if(is_feed()) { //隻在Feed中執行
		$output = '歡迎訪問 https://www.wpdaxue.com'; //添加自定義內容
		$content = $content . $output ; //重新設定文章內容 $content
	}
	return $content; //返回最後的文章內容
}
add_filter('the_content','custom_rss_feed_content'); //通過鉤子掛載該函數

function custom_rss_feed_content($content) { //定義新函數
if(is_feed()) { //隻在Feed中執行
$output = ‘歡迎訪問 https://www.wpdaxue.com’; //添加自定義內容
$content = $content . $output ; //重新設定文章內容 $content
}
return $content; //返回最後的文章內容
}
add_filter(‘the_content’,’custom_rss_feed_content’); //通過鉤子掛載該函數

註:

1. 代碼中的 $content 是WordPress預留的 文章內容變量,$output 是我們自定義的變量,用來添加自定義內容;

2. $content . $output 表示在文章原文的後面添加 $output 的內容,如果你想在原文前面添加,可以改為 $output . $content

3. $output 後面的自定義內容可以是 HTML 代碼,比如下面的例子:

1
2
3
4
5
6
7
8
9
10
11
//Feed輸出版權信息
function wpdaxue_feed_copyright($content) {
	if(is_feed()) {
		$post_title = get_the_title(); //獲取原文標題
		$post_link = get_permalink($post->ID); //獲取原文鏈接
		$output = '<p><span style="font-weight:bold;text-shadow:0 1px 0 #ddd;">聲明:</span> 本文采用 <a rel="nofollow" href="http://creativecommons.org/licenses/by-nc-sa/3.0/" title="署名-非商業性使用-相同方式共享">BY-NC-SA</a> 協議進行授權 | <a href="'.home_url().'">'.get_bloginfo('name').'</a><br />轉載請註明轉自《<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輸出版權信息
function wpdaxue_feed_copyright($content) {
if(is_feed()) {
$post_title = get_the_title(); //獲取原文標題
$post_link = get_permalink($post->ID); //獲取原文鏈接
$output = ‘<p><span style="font-weight:bold;text-shadow:0 1px 0 #ddd;">聲明:</span> 本文采用 <a rel="nofollow" href="http://creativecommons.org/licenses/by-nc-sa/3.0/" title="署名-非商業性使用-相同方式共享">BY-NC-SA</a> 協議進行授權 | <a href="’.home_url().’">’.get_bloginfo(‘name’).'</a><br />轉載請註明轉自《<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() 函數獲取自定義字段的值。假設你要調用的是 copyright 這個自定義字段,可以使用下面的代碼:

1
2
3
4
5
6
7
8
9
10
//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 輸出自定義字段
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 輸出文章特色圖像

1
2
3
4
5
6
7
8
9
10
11
//Feed 輸出文章特色圖像(縮略圖)
function rss_post_thumbnail($content) {
	global $post; //查詢全局文章
	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 輸出文章特色圖像(縮略圖)
function rss_post_thumbnail($content) {
global $post; //查詢全局文章
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 隻輸出簡碼內容

1
2
3
4
5
6
//Feed 隻輸出簡碼(shortcode)內容
function rssonly_content( $atts, $content = null) {
	if (!is_feed()) return "";//如果不是Feed,不返回內容
	return $content;
}
add_shortcode('rssonly', 'rssonly_content'); //註冊簡碼 rssonly

//Feed 隻輸出簡碼(shortcode)內容
function rssonly_content( $atts, $content = null) {
if (!is_feed()) return "";//如果不是Feed,不返回內容
return $content;
}
add_shortcode(‘rssonly’, ‘rssonly_content’); //註冊簡碼 rssonly

在寫文章的時候,使用簡碼 [rssonly] 包含的內容,隻會在Feed輸出:

1
[rssonly] 非常感謝訪問 WordPress大學 www.wpdaxue.com [/rssonly]

[rssonly] 非常感謝訪問 WordPress大學 www.wpdaxue.com [/rssonly]

在 Feed 中排除分類

1
2
3
4
5
6
7
8
//在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');

//在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’);

如果要排除多個分類,將第 4 行修改為下面的代碼:

1
$query->set('cat','-1, -4, -7'); //排除ID為 1、4、7 的分類

$query->set(‘cat’,’-1, -4, -7′); //排除ID為 1、4、7 的分類

Feed 輸出自定義文章類型的內容

請移步閱讀《讓WordPress RSS Feed輸出自定義文章類型的內容》

禁用所有 Feed 訂閱

如果你不願意讓別人訂閱的你網站,可以使用下面的代碼:

1
2
3
4
5
6
7
8
9
//禁用Feed訂閱
function wp_disable_feed() {
	wp_die( __('抱歉,本站不支援訂閱,請返回<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);

//禁用Feed訂閱
function wp_disable_feed() {
wp_die( __(‘抱歉,本站不支援訂閱,請返回<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);

好瞭,今天就分享這些,如果你還知道其他Feed優化技巧,歡迎和我們一起分享。

相關推薦:

3 個 WordPress Feed訂閱統計外掛

WordPress禁止采集RSS內容的外掛:Block RSS Reading

發佈留言

發佈留言必須填寫的電子郵件地址不會公開。 必填欄位標示為 *