【问题标题】:Exclude category from main RSS feed, but not all feeds从主要 RSS 提要中排除类别,但不是所有提要
【发布时间】:2012-10-06 19:01:49
【问题描述】:

我有一个博客,它通过 RSS 向多个 MailChimp 时事通讯提供内容。第一份时事通讯工作正常,但第二份我遇到了问题。我遇到的问题是第二个时事通讯有“隐藏”的内容。此内容不适合广泛使用,因此不会出现在首页上,但可以在网站的其他地方访问。与此有关的问题是,并非所有这些内容都被隐藏,只是其中的一些内容,而此时事通讯的其他内容可能与第一个时事通讯重叠。这显然会使排除所有内容成为问题,因为它们可能被分配多个类别,其中一些我不想隐藏。

我遇到的问题是我有办法将这些内容从首页中排除,但不能从主 RSS 提要中排除。我为此使用了 WP Hide Post,它允许我从提要中排除,进而将其从所有提要中删除,包括提供新闻通讯的提要。我目前正在使用 /feed?cat=XXX 来引用这些提要。有没有办法让类别提要仍然有效,只是主 /feed RSS 会排除它?

【问题讨论】:

    标签: wordpress rss


    【解决方案1】:

    我不确定你是否想要这个。我猜您想从您的rss 提要中排除一个或多个类别,为此您可以在您的functions.php 中添加此代码 sn-p

    function myFeedExcluder($query) {
        if ($query->is_feed) {
           $query->set('cat','-10'); // excludes category with id 10
        }                            // for multiple you can use ‘-10,-20,-30′  
        return $query;
    }
    add_filter('pre_get_posts','myFeedExcluder');
    

    也可以直接在链接中使用

    <a href="<?php bloginfo('url'); ?>/feed?cat=-10&cat=-20">Entries (RSS)</a>
    <a href="<?php bloginfo('url'); ?>/feed=rss2&cat=-10,-20">Entries (RSS)</a>
    

    您也可以检查此plugin 以从feed 中排除一些帖子,但我没有测试它,这里是another article 关于排除RSS Feed And More 中的类别。

    【讨论】:

    • 这是我的问题的一部分。当您在 functions.php 中排除时,这是整个站点的更改,因为 WP 基本上将其 RSS 提要视为一种设置,而不是区分类别提要或任何东西。所以我会排除一个类别,然后将无法访问该类别提要。
    【解决方案2】:

    我最终不得不从另一个角度重新考虑这个问题。我不得不为我的每个类别创建独特的提要,而不是能够从主要提要中排除帖子。这使得这些提要独立于主要提要,并且不会受到 WP Hide Posts 插件的影响。

    我所做的是将这个文件放到我的主题文件夹中,然后使用分配给它的模板创建空白页面。在顶部,我让它处理我创建的每个页面并将其分配给相应的类别。

    <?php 
    /* 
    Template Name: Custom feeds
    */
    $numposts = 10; // number of posts in feed
    
    if ( is_page( 'custom-feed-1' )) {    
        $posts = query_posts('showposts='.$numposts.'&cat=1'); 
    } elseif ( is_page( 'custom-feed-2' )) {  
        $posts = query_posts('showposts='.$numposts.'&cat=2');  
    } elseif ( is_page( 'custom-feed-3' )) {  
        $posts = query_posts('showposts='.$numposts.'&cat=3'); 
    }
    
    $more = 1;
    
    header('Content-Type: '.feed_content_type('rss-http').'; charset='.get_option('blog_charset'), true);
    echo '<?xml version="1.0" encoding="'.get_option('blog_charset').'"?'.'>';
    ?>
    
    <rss version="2.0"
      xmlns:content="http://purl.org/rss/1.0/modules/content/"
      xmlns:wfw="http://wellformedweb.org/CommentAPI/"
      xmlns:dc="http://purl.org/dc/elements/1.1/"
      xmlns:atom="http://www.w3.org/2005/Atom"
      xmlns:sy="http://purl.org/rss/1.0/modules/syndication/"
      xmlns:slash="http://purl.org/rss/1.0/modules/slash/"
      xmlns:media="http://search.yahoo.com/mrss/">
    
    <channel>
      <title><?php bloginfo_rss('name'); wp_title_rss(); ?> - Article Feed</title>
      <atom:link href="<?php self_link(); ?>" rel="self" type="application/rss+xml" />
      <link><?php bloginfo_rss('url') ?></link>
      <description><?php bloginfo_rss("description") ?></description>
      <lastBuildDate><?php echo mysql2date('D, d M Y H:i:s +0000', get_lastpostmodified('GMT'), false); ?></lastBuildDate>
      <?php the_generator( 'rss2' ); ?>
      <language>en-US</language>
      <sy:updatePeriod><?php echo apply_filters( 'rss_update_period', 'hourly' ); ?></sy:updatePeriod>
      <sy:updateFrequency><?php echo apply_filters( 'rss_update_frequency', '1' ); ?></sy:updateFrequency>
      <?php do_action('rss2_head'); ?>
      <?php while( have_posts()) : the_post(); ?>
    
      <item>
        <title><?php the_title_rss(); ?></title>
        <link><?php the_permalink_rss(); ?></link>
        <comments><?php comments_link(); ?></comments>
        <pubDate><?php echo mysql2date('D, d M Y H:i:s +0000', get_post_time('Y-m-d H:i:s', true), false); ?></pubDate>
        <dc:creator><?php the_author(); ?></dc:creator>
    <?php the_category_rss(); ?>
        <guid isPermaLink="false"><?php the_guid(); ?></guid>
    <?php if (get_option('rss_use_excerpt')) : ?>
        <description><![CDATA[<?php the_excerpt_rss() ?>]]></description>
    <?php else : ?>
        <description><![CDATA[<?php the_excerpt_rss() ?>]]></description>
      <?php if ( strlen( $post->post_content ) > 0 ) : ?> 
        <content:encoded><![CDATA[<?php the_content() ?>]]></content:encoded>
      <?php else : ?>
        <content:encoded><![CDATA[<?php the_excerpt_rss() ?>]]></content:encoded>
      <?php endif; ?>
    <?php endif; ?>
    
    <?php do_action('rss2_item'); ?>
    
      </item>
      <?php endwhile; ?>
    
    </channel>
    </rss>
    

    另外我不想声称这个模板,我之前在网上找到了它并稍微修改了它,我只是不记得我在哪里看到它。

    【讨论】:

      【解决方案3】:
      add_action('pre_get_posts', 'exclude_category' );
      function exclude_category( &$wp_query ) 
          // Exclude from loop, archive and feed but not from category page/feed
         if( is_home() || ( is_feed() && !is_category() ) || ( is_archive() && !is_category() )) { // Exclude from home, feed, but not from category page/feed
              set_query_var('category__not_in', array(120)); // Exclude category with ID 120
          }
      }
      

      【讨论】:

      • 这看起来是一个优雅的解决方案,必须试一试。
      【解决方案4】:

      谢谢!这可以完美地从主 rss 提要中排除一个类别,但仍将其包含在类别 rss 提要中。

      但是,当我将代码插入functions.php 时,代码中有一个小的语法错误导致我的网站崩溃。它缺少 if 语句的左“{”括号。这是更正后的代码:

      add_action('pre_get_posts', 'exclude_category' );
      function exclude_category( &$wp_query ) 
          // Exclude from loop, archive and feed but not from category page/feed
         { if( is_home() || ( is_feed() && !is_category() ) || ( is_archive() && !is_category() )) { // Exclude from home, feed, but not from category page/feed
              set_query_var('category__not_in', array(120)); // Exclude category with ID 120
          }
      }
      

      当然,记得将“120”更改为您的类别 ID。

      再次感谢您。这太有帮助了!

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2013-01-29
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2017-11-18
        • 1970-01-01
        相关资源
        最近更新 更多