問題描述

我想在 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 輔助翻譯的英文資料結果。如果您對結果不滿意,可以加入我們改善翻譯效果:薇曉朵技術論壇。