自定义帖子类型提要的问题

时间:2016-04-01 作者:Sabbir Hasan

显示主提要内容的自定义帖子类型提要url。

详细信息:我的网站上有一些自定义帖子类型。我使用这个片段将它们添加到主提要

function mycustomfeed_cpt_feed( $query ) {

  // Add to my custom-type post type
  $types = array(\'post\',\'blood-blog\');

        if ( $query->is_feed() )
            $query->set( \'post_type\', $types ); 
        return $query;
}
add_filter( \'pre_get_posts\', \'mycustomfeed_cpt_feed\' );
下面是我注册帖子类型的方式

/*Register Blood Blog*/
function mcp_blood_blog() {
    $labels = array(
        \'name\'               => _x( \'Blood Blog\', \'post type general name\' ),
        \'singular_name\'      => _x( \'Blood Blog\', \'post type singular name\' ),
        \'add_new\'            => _x( \'Add New\', \'Blood Blog\' ),
        \'add_new_item\'       => __( \'Add New Blog\' ),
        \'edit_item\'          => __( \'Edit\' ),
        \'new_item\'           => __( \'New Blood Blog\' ),
        \'all_items\'          => __( \'Blood Blog\' ),
        \'view_item\'          => __( \'View Blog\' ),
        \'search_items\'       => __( \'Search Blog\' ),
        \'not_found\'          => __( \'No Blog Found\' ),
        \'not_found_in_trash\' => __( \'No Blog Found Trash\' ),
        \'parent_item_colon\'  => \'\',
        \'menu_name\'          => \'Blood Blog\'
    );
    $args = array(
        \'labels\'        => $labels,
        \'description\'   => \'Insert A Blood Blog\',
        \'public\'        => true,
        \'menu_position\' => 5,
        \'menu_icon\'     => \'dashicons-heart\',
        \'supports\'      => array( \'title\', \'editor\', \'thumbnail\', \'excerpt\', \'comments\', \'author\' ),
        \'has_archive\'   => true,
        \'hierarchical\'  => false,
        \'rewrite\'       => array(\'slug\' => \'blood/blog\',\'with_front\' => true,\'feeds\' => true,),
        \'query_var\'     => true,
        \'publicly_queryable\'    => true,
        \'public\'                => true,
        \'show_in_admin_bar\' => true,
        \'show_in_nav_menus\' => false,
        \'capability_type\' => \'post\',
        \'_edit_link\' => \'post.php?post=%d\' ,
    );
    register_post_type( \'blood-blog\', $args );
}
add_action( \'init\', \'mcp_blood_blog\' );
/*End Blood Blog*/
问题是,当我尝试从如下自定义帖子类型获取提要时:

http://www.mediwaretesting.com/blood/blog/feed/

它返回已在中的主提要内容

http://www.mediwaretesting.com/feed/

注:post type slug中的“blood/blog”。

1 个回复
最合适的回答,由SO网友:birgire 整理而成

请注意$query->is_feed() 对于两个提要都是如此,所以您要使用pre_get_posts

我快速浏览了一下WP_Query 对象,并且如果您想要组合这两种帖子类型,则仅针对主提要example.tld/feed/, 您可以尝试:

add_filter( \'pre_get_posts\', function( \\WP_Query $q )
{
    if ( 
            $q->is_feed() 
         && ! $q->is_archive() 
         && ! $q->is_singular() 
         && ! $q->is_comment_feed() 
    )
        $q->set( \'post_type\', [ \'post\', \'blood-blog\' ] );       
} );
我可能漏掉了一些支票,所以你可能想再检查一遍。

附言:如果有is_feed_home()is_main_feed() 此处;-)

相关推荐