本文目录[隐藏]
- 1RSS Feed 基本设置
- 2Feed 输出自定义内容
- 3Feed 输出自定义字段
- 4Feed 输出文章特色图像
- 5Feed 只输出简码内容
- 6在 Feed 中排除分类
- 7Feed 输出自定义文章类型的内容
- 8禁用所有 Feed 订阅
挖鱼源码网之前已经介绍了 WordPress的RSS Feed地址是什么?如何添加?如何订阅?,今天补充一下 WordPress RSS Feed 设置及优化技巧。
在后台 > 设置 >阅读,可以设置 Feed 输出的篇数和类型:

注:如无特殊说明,下面的代码都添加到当前主题的 functions.php 文件即可
Feed 输出自定义内容
在feed中输出自定义内容可以通过 ‘the_content’ 这个 filter 钩子轻松实现,我们要做的就是使用 is_feed() 这个条件标签来判断只在 Feed 输出内容。例如下面的例子:
|
|
<span style="color: #000000; font-weight: bold;">function</span> custom_rss_feed_content<span style="color: #009900;">(</span><span style="color: #000088;">$content</span><span style="color: #009900;">)</span> <span style="color: #009900;">{</span> <span style="color: #666666; font-style: italic;">//定义新函数</span> <span style="color: #b1b100;">if</span><span style="color: #009900;">(</span>is_feed<span style="color: #009900;">(</span><span style="color: #009900;">)</span><span style="color: #009900;">)</span> <span style="color: #009900;">{</span> <span style="color: #666666; font-style: italic;">//只在Feed中执行</span> <span style="color: #000088;">$output</span> <span style="color: #339933;">=</span> <span style="color: #0000ff;">'欢迎访问 https://www.wpdaxue.com'</span><span style="color: #339933;">;</span> <span style="color: #666666; font-style: italic;">//添加自定义内容</span> <span style="color: #000088;">$content</span> <span style="color: #339933;">=</span> <span style="color: #000088;">$content</span> <span style="color: #339933;">.</span> <span style="color: #000088;">$output</span> <span style="color: #339933;">;</span> <span style="color: #666666; font-style: italic;">//重新设定文章内容 $content</span> <span style="color: #009900;">}</span> <span style="color: #b1b100;">return</span> <span style="color: #000088;">$content</span><span style="color: #339933;">;</span> <span style="color: #666666; font-style: italic;">//返回最后的文章内容</span> <span style="color: #009900;">}</span> add_filter<span style="color: #009900;">(</span><span style="color: #0000ff;">'the_content'</span><span style="color: #339933;">,</span><span style="color: #0000ff;">'custom_rss_feed_content'</span><span style="color: #009900;">)</span><span style="color: #339933;">;</span> <span style="color: #666666; font-style: italic;">//通过钩子挂载该函数</span> |
|
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 代码,比如下面的例子:
|
|
<span style="color: #666666; font-style: italic;">//Feed输出版权信息</span> <span style="color: #000000; font-weight: bold;">function</span> wpdaxue_feed_copyright<span style="color: #009900;">(</span><span style="color: #000088;">$content</span><span style="color: #009900;">)</span> <span style="color: #009900;">{</span> <span style="color: #b1b100;">if</span><span style="color: #009900;">(</span>is_feed<span style="color: #009900;">(</span><span style="color: #009900;">)</span><span style="color: #009900;">)</span> <span style="color: #009900;">{</span> <span style="color: #000088;">$post_title</span> <span style="color: #339933;">=</span> get_the_title<span style="color: #009900;">(</span><span style="color: #009900;">)</span><span style="color: #339933;">;</span> <span style="color: #666666; font-style: italic;">//获取原文标题</span> <span style="color: #000088;">$post_link</span> <span style="color: #339933;">=</span> get_permalink<span style="color: #009900;">(</span><span style="color: #000088;">$post</span><span style="color: #339933;">-></span><span style="color: #004000;">ID</span><span style="color: #009900;">)</span><span style="color: #339933;">;</span> <span style="color: #666666; font-style: italic;">//获取原文链接</span> <span style="color: #000088;">$output</span> <span style="color: #339933;">=</span> <span style="color: #0000ff;">'<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="'</span><span style="color: #339933;">.</span>home_url<span style="color: #009900;">(</span><span style="color: #009900;">)</span><span style="color: #339933;">.</span><span style="color: #0000ff;">'">'</span><span style="color: #339933;">.</span>get_bloginfo<span style="color: #009900;">(</span><span style="color: #0000ff;">'name'</span><span style="color: #009900;">)</span><span style="color: #339933;">.</span><span style="color: #0000ff;">'</a><br />转载请注明转自《<a rel="bookmark" title="'</span> <span style="color: #339933;">.</span> <span style="color: #000088;">$post_title</span> <span style="color: #339933;">.</span> <span style="color: #0000ff;">'" href="'</span> <span style="color: #339933;">.</span> <span style="color: #000088;">$post_link</span> <span style="color: #339933;">.</span> <span style="color: #0000ff;">'">'</span> <span style="color: #339933;">.</span> <span style="color: #000088;">$post_title</span> <span style="color: #339933;">.</span> <span style="color: #0000ff;">'</a>》</p>'</span><span style="color: #339933;">;</span> <span style="color: #000088;">$content</span> <span style="color: #339933;">=</span> <span style="color: #000088;">$content</span> <span style="color: #339933;">.</span> <span style="color: #000088;">$output</span> <span style="color: #339933;">;</span> <span style="color: #009900;">}</span> <span style="color: #b1b100;">return</span> <span style="color: #000088;">$content</span><span style="color: #339933;">;</span> <span style="color: #009900;">}</span> add_filter <span style="color: #009900;">(</span><span style="color: #0000ff;">'the_content'</span><span style="color: #339933;">,</span> <span style="color: #0000ff;">'wpdaxue_feed_copyright'</span><span style="color: #009900;">)</span><span style="color: #339933;">;</span> |
|
//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 这个自定义字段,可以使用下面的代码:
|
|
<span style="color: #666666; font-style: italic;">//Feed 输出自定义字段</span> <span style="color: #000000; font-weight: bold;">function</span> fields_in_feed<span style="color: #009900;">(</span><span style="color: #000088;">$content</span><span style="color: #009900;">)</span> <span style="color: #009900;">{</span> <span style="color: #b1b100;">if</span><span style="color: #009900;">(</span>is_feed<span style="color: #009900;">(</span><span style="color: #009900;">)</span><span style="color: #009900;">)</span> <span style="color: #009900;">{</span> <span style="color: #000088;">$post_id</span> <span style="color: #339933;">=</span> get_the_ID<span style="color: #009900;">(</span><span style="color: #009900;">)</span><span style="color: #339933;">;</span> <span style="color: #666666; font-style: italic;">//获取文章ID</span> <span style="color: #000088;">$output</span> <span style="color: #339933;">=</span> get_post_meta<span style="color: #009900;">(</span><span style="color: #000088;">$post_id</span><span style="color: #339933;">,</span> <span style="color: #0000ff;">'copyright'</span><span style="color: #339933;">,</span> <span style="color: #009900; font-weight: bold;">true</span><span style="color: #009900;">)</span> <span style="color: #339933;">;</span> <span style="color: #666666; font-style: italic;">// 获取字段 copyright 的值</span> <span style="color: #000088;">$content</span> <span style="color: #339933;">=</span> <span style="color: #000088;">$content</span><span style="color: #339933;">.</span><span style="color: #000088;">$output</span><span style="color: #339933;">;</span> <span style="color: #009900;">}</span> <span style="color: #b1b100;">return</span> <span style="color: #000088;">$content</span><span style="color: #339933;">;</span> <span style="color: #009900;">}</span> add_filter<span style="color: #009900;">(</span><span style="color: #0000ff;">'the_content'</span><span style="color: #339933;">,</span><span style="color: #0000ff;">'fields_in_feed'</span><span style="color: #009900;">)</span><span style="color: #339933;">;</span> |
|
//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 输出文章特色图像
|
|
<span style="color: #666666; font-style: italic;">//Feed 输出文章特色图像(缩略图)</span> <span style="color: #000000; font-weight: bold;">function</span> rss_post_thumbnail<span style="color: #009900;">(</span><span style="color: #000088;">$content</span><span style="color: #009900;">)</span> <span style="color: #009900;">{</span> <span style="color: #000000; font-weight: bold;">global</span> <span style="color: #000088;">$post</span><span style="color: #339933;">;</span> <span style="color: #666666; font-style: italic;">//查询全局文章</span> <span style="color: #b1b100;">if</span><span style="color: #009900;">(</span>has_post_thumbnail<span style="color: #009900;">(</span><span style="color: #000088;">$post</span><span style="color: #339933;">-></span><span style="color: #004000;">ID</span><span style="color: #009900;">)</span><span style="color: #009900;">)</span> <span style="color: #009900;">{</span> <span style="color: #666666; font-style: italic;">//如果有特色图像</span> <span style="color: #000088;">$output</span> <span style="color: #339933;">=</span> get_the_post_thumbnail<span style="color: #009900;">(</span><span style="color: #000088;">$post</span><span style="color: #339933;">-></span><span style="color: #004000;">ID</span><span style="color: #009900;">)</span> <span style="color: #339933;">;</span> <span style="color: #666666; font-style: italic;">//获取缩略图</span> <span style="color: #000088;">$content</span> <span style="color: #339933;">=</span> <span style="color: #000088;">$output</span> <span style="color: #339933;">.</span> <span style="color: #000088;">$content</span> <span style="color: #339933;">;</span> <span style="color: #009900;">}</span> <span style="color: #b1b100;">return</span> <span style="color: #000088;">$content</span><span style="color: #339933;">;</span> <span style="color: #009900;">}</span> add_filter<span style="color: #009900;">(</span><span style="color: #0000ff;">'the_excerpt_rss'</span><span style="color: #339933;">,</span> <span style="color: #0000ff;">'rss_post_thumbnail'</span><span style="color: #009900;">)</span><span style="color: #339933;">;</span> add_filter<span style="color: #009900;">(</span><span style="color: #0000ff;">'the_content_feed'</span><span style="color: #339933;">,</span> <span style="color: #0000ff;">'rss_post_thumbnail'</span><span style="color: #009900;">)</span><span style="color: #339933;">;</span> |
|
//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 只输出简码内容
|
|
<span style="color: #666666; font-style: italic;">//Feed 只输出简码(shortcode)内容</span> <span style="color: #000000; font-weight: bold;">function</span> rssonly_content<span style="color: #009900;">(</span> <span style="color: #000088;">$atts</span><span style="color: #339933;">,</span> <span style="color: #000088;">$content</span> <span style="color: #339933;">=</span> <span style="color: #009900; font-weight: bold;">null</span><span style="color: #009900;">)</span> <span style="color: #009900;">{</span> <span style="color: #b1b100;">if</span> <span style="color: #009900;">(</span><span style="color: #339933;">!</span>is_feed<span style="color: #009900;">(</span><span style="color: #009900;">)</span><span style="color: #009900;">)</span> <span style="color: #b1b100;">return</span> <span style="color: #0000ff;">""</span><span style="color: #339933;">;</span><span style="color: #666666; font-style: italic;">//如果不是Feed,不返回内容</span> <span style="color: #b1b100;">return</span> <span style="color: #000088;">$content</span><span style="color: #339933;">;</span> <span style="color: #009900;">}</span> add_shortcode<span style="color: #009900;">(</span><span style="color: #0000ff;">'rssonly'</span><span style="color: #339933;">,</span> <span style="color: #0000ff;">'rssonly_content'</span><span style="color: #009900;">)</span><span style="color: #339933;">;</span> <span style="color: #666666; font-style: italic;">//注册简码 rssonly</span> |
|
//Feed 只输出简码(shortcode)内容
function rssonly_content( $atts, $content = null) {
if (!is_feed()) return "";//如果不是Feed,不返回内容
return $content;
}
add_shortcode(‘rssonly’, ‘rssonly_content’); //注册简码 rssonly
在写文章的时候,使用简码 [rssonly] 包含的内容,只会在Feed输出:
|
|
<span style="color: #009900;">[</span>rssonly<span style="color: #009900;">]</span> 非常感谢访问 WordPress大学 www<span style="color: #339933;">.</span>wpdaxue<span style="color: #339933;">.</span>com <span style="color: #009900;">[</span><span style="color: #339933;">/</span>rssonly<span style="color: #009900;">]</span> |
|
[rssonly] 非常感谢访问 WordPress大学 www.wpdaxue.com [/rssonly]
在 Feed 中排除分类
|
|
<span style="color: #666666; font-style: italic;">//在Feed中排除某些分类</span> <span style="color: #000000; font-weight: bold;">function</span> exclude_cat_feed<span style="color: #009900;">(</span><span style="color: #000088;">$query</span><span style="color: #009900;">)</span> <span style="color: #009900;">{</span> <span style="color: #b1b100;">if</span><span style="color: #009900;">(</span>is_feed<span style="color: #009900;">(</span><span style="color: #009900;">)</span><span style="color: #009900;">)</span> <span style="color: #009900;">{</span> <span style="color: #000088;">$query</span><span style="color: #339933;">-></span><span style="color: #004000;">set</span><span style="color: #009900;">(</span><span style="color: #0000ff;">'cat'</span><span style="color: #339933;">,</span><span style="color: #0000ff;">'-1'</span><span style="color: #009900;">)</span><span style="color: #339933;">;</span> <span style="color: #666666; font-style: italic;">//排除ID为 1 的分类</span> <span style="color: #b1b100;">return</span> <span style="color: #000088;">$query</span><span style="color: #339933;">;</span> <span style="color: #009900;">}</span> <span style="color: #009900;">}</span> add_filter<span style="color: #009900;">(</span><span style="color: #0000ff;">'pre_get_posts'</span><span style="color: #339933;">,</span> <span style="color: #0000ff;">'exclude_cat_feed'</span><span style="color: #009900;">)</span><span style="color: #339933;">;</span> |
|
//在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 行修改为下面的代码:
|
|
<span style="color: #000088;">$query</span><span style="color: #339933;">-></span><span style="color: #004000;">set</span><span style="color: #009900;">(</span><span style="color: #0000ff;">'cat'</span><span style="color: #339933;">,</span><span style="color: #0000ff;">'-1, -4, -7'</span><span style="color: #009900;">)</span><span style="color: #339933;">;</span> <span style="color: #666666; font-style: italic;">//排除ID为 1、4、7 的分类</span> |
|
$query->set(‘cat’,’-1, -4, -7′); //排除ID为 1、4、7 的分类
Feed 输出自定义文章类型的内容
请移步阅读《让WordPress RSS Feed输出自定义文章类型的内容》
禁用所有 Feed 订阅
如果你不愿意让别人订阅的你网站,可以使用下面的代码:
|
|
<span style="color: #666666; font-style: italic;">//禁用Feed订阅</span> <span style="color: #000000; font-weight: bold;">function</span> wp_disable_feed<span style="color: #009900;">(</span><span style="color: #009900;">)</span> <span style="color: #009900;">{</span> wp_die<span style="color: #009900;">(</span> __<span style="color: #009900;">(</span><span style="color: #0000ff;">'抱歉,本站不支持订阅,请返回<a href="'</span><span style="color: #339933;">.</span> get_bloginfo<span style="color: #009900;">(</span><span style="color: #0000ff;">'url'</span><span style="color: #009900;">)</span> <span style="color: #339933;">.</span><span style="color: #0000ff;">'">首页</a>'</span><span style="color: #009900;">)</span> <span style="color: #009900;">)</span><span style="color: #339933;">;</span> <span style="color: #009900;">}</span> add_action<span style="color: #009900;">(</span><span style="color: #0000ff;">'do_feed'</span><span style="color: #339933;">,</span> <span style="color: #0000ff;">'wp_disable_feed'</span><span style="color: #339933;">,</span> <span style="color: #cc66cc;">1</span><span style="color: #009900;">)</span><span style="color: #339933;">;</span> add_action<span style="color: #009900;">(</span><span style="color: #0000ff;">'do_feed_rdf'</span><span style="color: #339933;">,</span> <span style="color: #0000ff;">'wp_disable_feed'</span><span style="color: #339933;">,</span> <span style="color: #cc66cc;">1</span><span style="color: #009900;">)</span><span style="color: #339933;">;</span> add_action<span style="color: #009900;">(</span><span style="color: #0000ff;">'do_feed_rss'</span><span style="color: #339933;">,</span> <span style="color: #0000ff;">'wp_disable_feed'</span><span style="color: #339933;">,</span> <span style="color: #cc66cc;">1</span><span style="color: #009900;">)</span><span style="color: #339933;">;</span> add_action<span style="color: #009900;">(</span><span style="color: #0000ff;">'do_feed_rss2'</span><span style="color: #339933;">,</span> <span style="color: #0000ff;">'wp_disable_feed'</span><span style="color: #339933;">,</span> <span style="color: #cc66cc;">1</span><span style="color: #009900;">)</span><span style="color: #339933;">;</span> add_action<span style="color: #009900;">(</span><span style="color: #0000ff;">'do_feed_atom'</span><span style="color: #339933;">,</span> <span style="color: #0000ff;">'wp_disable_feed'</span><span style="color: #339933;">,</span> <span style="color: #cc66cc;">1</span><span style="color: #009900;">)</span><span style="color: #339933;">;</span> |
|
//禁用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
赏
如果本文对你有帮助,请打赏作者,鼓励我们继续写作!
请先
!