在RSS中将文本添加到帖子标题(自定义帖子类型)

时间:2011-10-25 作者:James Forbes

首先,我使用此代码将自定义帖子类型添加到我的提要中(如下所示):

function myfeed_request($qv) {
    if (isset($qv[\'feed\']))
        $qv[\'post_type\'] = get_post_types();
    return $qv;
}
add_filter(\'request\', \'myfeed_request\');
后来,我遇到WPBeginner\'s Adding Additional Text to Post Titles in RSS 并决定修改以下代码:

function wpbeginner_titlerss($content) {
global $wp_query;
$postid = $wp_query->post->ID;
$gpost = get_post_meta($postid, \'guest_post\', true);
$spost = get_post_meta($postid, \'sponsored_post\', true);

if($gpost !== \'\') {
$content = \'Guest Post: \'.$content;
}
elseif ($spost !== \'\'){
$content = \'Sponsored Post: \'.$content;
}
else {
$content = $content;
}
return $content;
}
add_filter(\'the_title_rss\', \'wpbeginner_titlerss\');
为了适应RSS中自定义帖子类型的显示:

function wpbeginner_titlerss($content) {
global $wp_query;
$post_type = $wp_query->post_type;

if( get_post_type() == \'essays\' ) {
$content = \'Essay: \'.$content;
}
elseif ( get_post_type() == \'gallery\' ){
$content = \'Gallery: \'.$content;
}
elseif ( get_post_type() == \'games\' ){
$content = \'Game: \'.$content;
}
else {
$content = $content;
}
return $content;
}
add_filter(\'the_title_rss\', \'wpbeginner_titlerss\');
最后的代码并没有以我想要的方式显示我的自定义帖子类型,任何想法/帮助都将不胜感激!

1 个回复
SO网友:Chip Bennett

既然你在定义$post_type, 为什么不直接使用它呢?

function wpbeginner_titlerss($content) {
    global $wp_query;
    $post_type = $wp_query->post_type;

    if( $post_type == \'essays\' ) {
        $content = \'Essay: \'.$content;
    }
    elseif ( $post_type == \'gallery\' ){
        $content = \'Gallery: \'.$content;
    }
    elseif ( $post_type == \'games\' ){
        $content = \'Game: \'.$content;
    }
    else {
        $content = $content;
    }
    return $content;
}
add_filter( \'the_title_rss\', \'wpbeginner_titlerss\' );
(假设您已创建自定义帖子类型,essays, gallery, 和games.)

结束

相关推荐

Query Posts Creates 404 Error

当我在档案页面时,我使用下面的代码来查询帖子。在我的主题选项to\\u count\\u home中调用帖子的数量。WordPress没有根据每页的posts\\u生成正确的页数。相反,它是根据我在默认WordPress设置中选择的每页帖子数生成页面。例如,如果我在设置中每页有10篇文章,那么在代码中将posts\\u per\\u page设置为5,当我转到第二页(应该有5篇文章)时,我会收到404错误。下面是我使用的代码:<?php $per_page = get_option(\'to