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

本人擅长Ai、Fw、Fl、Br、Ae、Pr、Id、Ps等软件的安装与卸载,精通CSS、JavaScript、PHP、ASP、C、C++、C#、Java、Ruby、Perl、Lisp、Python、Objective-C、ActionScript、Pascal等单词的拼写,熟悉Windows、Linux、OS X、Android、iOS、WP8等系统的开关机。

通过下面的方式来联系我们:

电邮:138762189@qq.com

联系QQ:点击这里给我发消息

官方站:www.tadke.com

※ ※ 联系请加我的企鹅号 ※※

※ ※技术支持请微信联系站长 ※※

Copyright © 2023 Tadke.com. 琼ICP备20000547号