2012-10-16 35 views
0

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

我遇到的问题是,我有一种方法可以将这些内容从首页中排除,但不能从主要的RSS提要中排除。我使用的是WP Hide Post,它允许我从Feed中排除,这反过来将它从所有提要中删除,包括提供时事通讯的提要。我目前使用/ feed?cat = XXX来引用这些提要。有没有一种方法可以使分类饲料仍然有效,只是主/饲料RSS会排除它?

回答

0

我最终不得不从另一个角度重新考虑这个问题。我不必从主要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> 

此外,我不希望要求打好这个模板,我在网上找到的一段时间回来,并修改了一点,我只是不记得我碰到它哪里传来。

0

我不确定你是否想要这个。我猜你想从你的rss饲料排除一个或多个类别,并做到这一点,你可以在你的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> 

你也可以检查这个pluginfeed排除一些帖子,但我没有测试它,这里是another article关于排除RSS Feed And More的类别。

+0

这是我的问题的一部分。当你在functions.php中排除它时,整个网站都会发生变化,因为WP基本上将RSS源视为一个设置,而不是区分类别源或任何内容。所以我会排除一个类别,然后将无法访问该类别的饲料。 – jbwharris

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

这看起来像一个优雅的解决方案,将不得不尝试一下。 – jbwharris

0

谢谢!这很好地从主要rss feed中排除一个类别,但仍然将其包含在rss feed类别中。

但是,当我将它插入到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。

再次感谢您。这非常有帮助!