从主RSS提要中排除类别,但不是所有提要

时间:2012-10-16 15:58:12

标签: wordpress rss

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

我遇到的问题是我有办法从首页排除这些内容,但不能从主RSS源中排除。我正在使用WP Hide Post,这允许我从Feed中排除,后者又将其从所有Feed中删除,包括提供简报的Feed。我目前正在使用/ feed?cat = XXX来引用这些Feed。有没有一种方法可以使类别提要仍然有效,只有主要/提要RSS会排除它?

4 个答案:

答案 0 :(得分:0)

我不确定你是否想要这个。我猜您要从rss Feed中排除一个或多个类别,为此您可以在functions.php

中添加此代码段
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排除类别的信息。

答案 1 :(得分:0)

我最后不得不从另一个角度重新思考这个问题。我不得不为主要Feed添加帖子,而是必须为每个类别创建唯一的Feed。这使得这些Feed独立于主要Feed并且不会受到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>

此外,我不想声称这个模板,我在网上发现了一段时间后又修改了一下,我只是不记得我遇到过它。

答案 2 :(得分:0)

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
    }
}

答案 3 :(得分:0)

谢谢!这非常适合从主rss Feed中排除某个类别,但仍将其包含在rss feed类别中。

但是,当我将其插入functions.php时,代码中出现了一个小的语法错误,导致我的网站崩溃。它错过了开幕式&#39; {&#39; 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
    }
}

当然,请记得改变&#39; 120&#39;到您的类别ID。

再次感谢你。这太有用了!