2014-05-21 50 views
0

我有一個自定義帖子類型,具有頁面功能,可通過WordPress創建HTML新聞稿。我正在使用具有子帖子的層次結構來創建雜誌類型的通訊:父帖子是主帖子,子項是下面的媒體對象。WordPress:排除CPT RSS feed中的子頁面

見例如:http://blog.utc.edu/today/newsletters/whats-the-latest-for-summer-semester-2014/

我需要爲這個CPT RSS feed,但這次我想排除孩子的帖子。是一個簡單的URL參數來實現這一目標? (我知道我可以創建自定義RSS模板。)

見例如:http://blog.utc.edu/today/feed/?post_type=utcblogs_newsletter&include_children=false

上面的網址是「樂觀」,我知道這個參數是不正確的。您可以看到包含子帖子。

我也由CPT添加到一般的RSS提要:

// Add newsletters to general RSS feed 
function add_cpt_to_feed($query) { 
    if (isset($query['feed']) && !isset($query['post_type'])) 
    $query['post_type'] = array('post', 'utcblogs_newsletter'); 
    return $query; 
} 
add_filter('request', 'add_cpt_to_feed'); 

那麼,有沒有修改$查詢排除孩子的帖子的方式?或者我需要一個不同的查詢或WP功能爲了達成這個?

+0

...刪除冗餘... – PlayGod

回答

0

我通過爲Custom Post Type創建自定義RSS模板來解決此問題。

要刪除的帖子的孩子(是的,帖子可以在一個CPT層次),使用post_parent

$args = array( 
      'post_type' => 'utcblogs_newsletter', 
      'post_parent' => 0 
     ); 
query_posts($args); 

所以,完整的自定義RSS模板(在主旋律文件夾通訊-customfeed.php)是:

<?php 
/* 
Template Name: Custom Newsletter Feed 

*/ 

$numposts = 5; 

function custom_rss_date($timestamp = null) { 
    $timestamp = ($timestamp==null) ? time() : $timestamp; 
    echo date(DATE_RSS, $timestamp); 
} 

function custom_rss_text_limit($string, $length, $replacer = '&hellip;') { 
    $string = strip_tags($string); 
    if(strlen($string) > $length) 
    return (preg_match('/^(.*)\W.*$/', substr($string, 0, $length+1), $matches) ? $matches[1] : substr($string, 0, $length)) . $replacer; 
    return $string; 
} 

$args = array( 
       'post_type' => 'utcblogs_newsletter', 
       'post_parent' => 0 
      ); 
query_posts($args); 

$lastpost = $numposts - 1; 



header('Content-Type: ' . feed_content_type('rss-http') . '; charset=' . get_option('blog_charset'), true); 
$more = 1; 

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/" 
    <?php do_action('rss2_ns'); ?> 
> 

<channel> 
    <title><?php bloginfo_rss('name'); wp_title_rss(); ?></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> 
    <language><?php bloginfo_rss('language'); ?></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_feed(); ?></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('rss2') ?> 
     <guid isPermaLink="false"><?php the_guid(); ?></guid> 
<?php if (get_option('rss_use_excerpt')) : ?> 
     <description><![CDATA[<?php get_the_excerpt(); ?>]]></description> 
<?php else : ?> 
     <description><?php echo '<![CDATA['.custom_rss_text_limit($post->post_content, 256).']]>'; ?></description> 
<?php $content = get_the_content_feed('rss2'); ?> 
    <?php if (strlen($content) > 0) : ?> 
     <content:encoded><![CDATA[<?php echo $content; ?>]]></content:encoded> 
    <?php else : ?> 
     <content:encoded><![CDATA[<?php the_excerpt(); ?>]]></content:encoded> 
    <?php endif; ?> 
<?php endif; ?> 
     <wfw:commentRss><?php echo esc_url(get_post_comments_feed_link(null, 'rss2')); ?></wfw:commentRss> 
     <slash:comments><?php echo get_comments_number(); ?></slash:comments> 
<?php rss_enclosure(); ?> 
<?php do_action('rss2_item'); ?> 
     </item> 
    <?php endwhile; ?> 
</channel> 
</rss> 

...和飼料URL在functions.php的設定並稱爲http://my.site.com/newsletter.xml

/* Custom RSS Feed for Newsletter CPT */ 

function create_my_customfeed_newsletter() { 
load_template(get_stylesheet_directory() . '/newsletter-customfeed.php'); 
} 
add_action('do_feed_newsletter', 'create_my_customfeed_newsletter', 10, 1); 

// creates /feed/feedname and /feedname.xml for each custom CPT RSS 
function custom_feed_rewrite($wp_rewrite) { 
$feed_rules = array(
'feed/(.+)' => 'index.php?feed=' . $wp_rewrite->preg_index(1), 
'(.+).xml' => 'index.php?feed='. $wp_rewrite->preg_index(1) 
); 
$wp_rewrite->rules = $feed_rules + $wp_rewrite->rules; 
} 
add_filter('generate_rewrite_rules', 'custom_feed_rewrite'); 

...幷包括一般的通訊RSS:

// Add newsletters to general RSS feed 
function add_cpt_to_feed($query) { 
    if (isset($query['feed']) && !isset($query['post_type'])) 
    $query['post_type'] = array(
           'post', 
           'utcblogs_newsletter', 
           'post_parent' => 0 
           ); 
    return $query; 
} 
add_filter('request', 'add_cpt_to_feed');