问题描述

我想在 feed 中显示每个页面的整个内容。我搜索并找到一些插件,但是我无法解决我的问题。

我想当我进入 http://swissaudio.com/craftsmanship/feed 它提供了我的页面内容在饲料。我怎样才能做到这一点?

最佳解决方案

首先将帖子类型设置为在主 Feed 页面上显示,即/feed 使用 pre_get_posts 钩子

$q->set('post_type', array('post', 'page'));

单独页面 WordPress 会显示注释信息,然后将其设置为 false,并显示 Feed 中的页面内容。

$q->is_comment_feed = false;

在 Feed 模板 WordPress 调用 the_excerpt_rss()调用 get_the_excerpt(),所以使用 excerpt_length 过滤器将长度更改为最大。

完整例子:

add_action('pre_get_posts', 'wpse_227136_feed_content');
/**
 * Set post type in feed content and remove comment feed
 * @param type $q WP Query
 */
function wpse_227136_feed_content($q) {
    //Check if it main query and for feed
    if ($q->is_main_query() && $q->is_feed()) {
        //Set the post types which you want default is post
        $q->set('post_type', array('post', 'page'));
    }

    //Check if it feed request and for single page
    if ($q->is_main_query() && $q->is_feed() && $q->is_page()) {
        //Set the comment feed to false
        $q->is_comment_feed = false;
    }
}

add_filter( 'excerpt_length', 'wpse_227136_excerpt_length', 999 );
/**
 * Filter the except length to full content.
 *
 * @param int $length Excerpt length.
 * @return int $length modified excerpt length.
 */
function wpse_227136_excerpt_length( $length ) {
    if (is_feed() && !get_option('rss_use_excerpt')) {
        return PHP_INT_MAX;
    }

    return $length;
}

次佳解决方案

这可能不是理想的,但它是一个开始。首先确保 Feed 中的完整内容是:

function fullcontentfeed($content) {
    global $post;
    $content = $post->post_content;
    return $content;
    }
add_filter('the_excerpt_rss', 'fullcontentfeed');

然后你应该看到这个 URL 的完整的 feed

http://swissaudio.com/craftsmanship/feed/?withoutcomments=1

然后,您可以使用 add_rewrite_rule 将来自/feed /的访问者重定向到。远离理想,但也许是其他人开始工作的开始。

第三种解决方案

如 @Sumit 所提到的,你需要关闭一个页面的评论 Feed(我觉得很奇怪,因为默认的评论是在页面上关闭的)… 这是我最后的结果 (允许获取页面评论如果需要,请与?withcomments=1 一起饲养):

add_action('pre_get_posts', 'rss_page_feed_full_content');

function rss_page_feed_full_content($q) {
    // Check if it feed request and for single page
    if ($q->is_main_query() && $q->is_feed() && $q->is_page()) {
        //Set the comment feed to false
        $q->set('post_type', array('page'));
        // allow for page comments feed via ?withcomments=1
        if ( (isset($_GET['withcomments'])) && ($_GET['withcomments'] == '1') ) {return;}
        $q->is_comment_feed = false;
    }
}

但是,为了显示页面内容,由于 Feed 模板实际上会检查 rss_use_excerpt 来决定是否显示全文或摘要 (在 「设置 – > 阅读」 页面上设置),因此如果您希望为页面显示完整的内容,则需要覆盖 feed(以便您可以将主选项设置为任何您喜欢的帖子。) 否则,无论您做什么,内容可能会在 Feed 的描述字段中,而不是内容字段。

add_filter('pre_option_rss_use_excerpt', 'page_rss_excerpt_option');

function page_rss_excerpt_option($option) {
    // force full content output for pages
    if (is_page()) {return '0';}
    return $option;
}

最后,要获取 RSS 描述字段以显示页面摘录,您可能需要执行此操作 (这基本上是没有 strip_shortcodeswp_trim_excerpt 的副本) – 好吧,我还是做了,但可能是由于一些奇怪的短代码行为第一页正在测试:

add_filter('the_excerpt_rss','rss_page_excerpt');

function rss_page_excerpt($excerpt) {
    if (is_page()) {
        global $post; $text = $post->post_content;
        // removed this line otherwise got blank
        // $text = strip_shortcodes( $text );
        $text = apply_filters( 'the_content', $text );
        $text = str_replace(']]>', ']]>', $text);
        $excerpt_length = apply_filters( 'excerpt_length', 55 );
        $excerpt_more = apply_filters( 'excerpt_more', ' ' . '[…]' );
        $excerpt = wp_trim_words( $text, $excerpt_length, $excerpt_more );
    }
    return $excerpt;
}

参考文献

注:本文内容整合自 Google/Baidu/Bing 辅助翻译的英文资料结果。如果您对结果不满意,可以加入我们改善翻译效果:薇晓朵技术论坛。