外贸独立站的底层设计密码WordPress 成品站SaaS建站
当前位置:首页>WordPress建站>新手入门>7+ WordPress RSS Feed 设置及优化技巧

7+ WordPress RSS 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 输出内容。例如下面的例子:

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 代码,比如下面的例子:

//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 这个自定义字段,可以使用下面的代码:

//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; //查询全局文章
	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 只输出简码内容

//Feed 只输出简码(shortcode)内容
function rssonly_content( $atts, $content = null) {
	if (!is_feed()) return "";//如果不是Feed,不返回内容
	return $content;
}
add_shortcode('rssonly', 'rssonly_content'); //注册简码 rssonly

在写文章的时候,使用简码 [rssonly] 包含的内容,只会在Feed输出:

[rssonly] 非常感谢访问 WordPress大学 www.wpdaxue.com [/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');

如果要排除多个分类,将第 4 行修改为下面的代码:

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

Feed 输出自定义文章类型的内容

请移步阅读《让WordPress RSS Feed输出自定义文章类型的内容

禁用所有 Feed 订阅

如果你不愿意让别人订阅的你网站,可以使用下面的代码:

//禁用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输出
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优化技巧,欢迎和我们一起分享。

相关推荐:

3 个 WordPress Feed订阅统计插件

WordPress禁止采集RSS内容的插件:Block RSS Reading

声明:本站所有文章,如无特殊说明或标注,均为本站原创发布。任何个人或组织,在未征得本站同意时,禁止复制、盗用、采集、发布本站内容到任何网站、书籍等各类媒体平台。如若本站内容侵犯了原著者的合法权益,可联系我们进行处理。

给TA打赏
共{{data.count}}人
人已打赏
欢迎关注WordPress大学公众号 WPDAXUE
新手入门

如何在WordPress菜单中添加一个无链接的菜单项

2013-6-15 7:57:50

新手入门

更改WordPress语言设置(例如中文版和英文版转换)

2013-7-23 7:55:00

12 条回复 A文章作者 M管理员
  1. 大师傅

    有没有美化的代码啊

  2. 不错。学习了。

个人中心
购物车
优惠劵
今日签到
有新私信 私信列表
搜索