如何为每种自定义帖子类型创建单独的RSS提要

时间:2010-10-31 作者:Giraldi

如何为每个自定义帖子类型制作单独的RSS提要?

我在网上搜索了一下,找到了这样做的建议:

http://www.yoursite.com/feed/?post_type=book

我试过上面的方法,但没有成功!它只会再次将我带回自定义帖子类型存档页面。。。

有人知道我哪里做错了,或者我错过了什么吗???

仅供参考,我使用了Custom Post Permalinks 插件,允许对post-type归档页面使用特定于post-type的永久链接。不确定这是否会影响RSS源问题。

干杯

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

好的,感谢所有帮助我回答问题的人,但我终于找到了问题的解决方案。

对不起,我太天真了,但事实证明我template_redirect 添加了操作来指导与我的post types 各自的template files. 类似这样:

function my_template_redirect()
{
    global $wp, $wp_query;

    if ( ( $wp->query_vars["post_type"] == "news" ) && !is_single() )
    {
        if (have_posts())
        {
            include(TEMPLATEPATH . \'/news.php\');
            die();
        }
        else
        { $wp_query->is_404 = true; }
    }
}
add_action("template_redirect", \'my_template_redirect\');
显然,它还将我的帖子类型提要重定向到模板文件,因此“它只会再次将我带回自定义帖子类型归档页面…”

所以我只是添加了一个额外的过滤器来从这个重定向中排除提要,也就是说,我添加了!is_feed()if 声明:

if ( ( $wp->query_vars["post_type"] == "news" ) && !is_single() && !is_feed() )
。。。饲料又恢复了正常工作。我知道主题有问题。。。

再次感谢你们的帮助,伙计们!:)

SO网友:bueltge

添加小示例函数:

    add_action( \'request\', \'fb_add_to_feed\' );

    // add to post-feed
    function fb_add_to_feed($request) {

        if ( isset($request[\'feed\']) && !isset($request[\'post_type\']) ) {
            $request[\'post_type\'] = get_post_types( $args = array(
                \'public\'          => true,
                \'capability_type\' => \'post\'
            ) );
        }

        return $request;
    }
或者,您可以仅为自定义帖子类型创建自己的提要。有关草稿帖子的“草稿提要”,请参见以下示例。

<?php
/*
Plugin Name: Drafts Feed
Plugin URI:  http://bueltge.de/wordpress-feed-fuer-entwuerfe/829/
Description: Add a new Feed for drafts: <code>/?feed=draft</code>
Version:     0.2
Author:      Frank Bültge
Author URI:  http://bueltge.de/
Licence:     GPL
Last Change: 17.06.2009 10:50:19
*/

//avoid direct calls to this file, because now WP core and framework has been used
if ( !function_exists(\'add_action\') ) {
    header(\'Status: 403 Forbidden\');
    header(\'HTTP/1.1 403 Forbidden\');
    exit();
}

if ( function_exists(\'add_action\') ) {
    //WordPress definitions
    if ( !defined(\'WP_CONTENT_URL\') )
        define(\'WP_CONTENT_URL\', get_option(\'siteurl\') . \'/wp-content\');
    if ( !defined(\'WP_CONTENT_DIR\') )
        define(\'WP_CONTENT_DIR\', ABSPATH . \'wp-content\');
    if ( !defined(\'WP_PLUGIN_URL\') )
        define(\'WP_PLUGIN_URL\', WP_CONTENT_URL.\'/plugins\');
    if ( !defined(\'WP_PLUGIN_DIR\') )
        define(\'WP_PLUGIN_DIR\', WP_CONTENT_DIR.\'/plugins\');
    if ( !defined(\'PLUGINDIR\') )
        define( \'PLUGINDIR\', \'wp-content/plugins\' ); // Relative to ABSPATH.  For back compat.
    if ( !defined(\'WP_LANG_DIR\') )
        define(\'WP_LANG_DIR\', WP_CONTENT_DIR . \'/languages\');

    // plugin definitions
    define( \'FB_DF_BASENAME\', plugin_basename(__FILE__) );
    define( \'FB_DF_BASEFOLDER\', plugin_basename( dirname( __FILE__ ) ) );
    define( \'FB_DF_TEXTDOMAIN\', \'draft_feed\' );
}

if ( !class_exists(\'DraftFeed\') ) {
    class DraftFeed {

        // constructor
        function DraftFeed() {

            add_action( \'init\', array(&$this, \'add_draft_feed\') );
            if ( is_admin() ) {
                add_action( \'wp_dashboard_setup\', array(&$this, \'my_wp_dashboard_setup\') );
                add_action( \'admin_head\', array(&$this, \'add_my_css\') );
                add_action( \'admin_init\', array(&$this, \'textdomain\') );
            }
        }

        function textdomain() {

            if ( function_exists(\'load_plugin_textdomain\') ) {
                if ( !defined(\'WP_PLUGIN_DIR\') ) {
                    load_plugin_textdomain( FB_DF_TEXTDOMAIN, str_replace( ABSPATH, \'\', dirname(__FILE__) ) . \'/languages\' );
                } else {
                    load_plugin_textdomain( FB_DF_TEXTDOMAIN, false, dirname( plugin_basename(__FILE__) ) . \'/languages\' );
                }
            }
        }

        function my_wp_dashboard_recent_drafts( $drafts = false, $view_content = false ) {
            if ( $drafts )
                return;

            $drafts_query = new WP_Query( array(
                                                                                    \'post_type\' => \'post\',
                                                                                    \'post_status\' => \'draft\',
                                                                                    \'posts_per_page\' => 5,
                                                                                    \'orderby\' => \'modified\',
                                                                                    \'order\' => \'DESC\'
                                                                                    ) );
            $drafts =& $drafts_query->posts;

            if ( $drafts && is_array( $drafts ) ) {
                $list = array();
                foreach ( $drafts as $draft ) {
                    $url = get_edit_post_link( $draft->ID );
                    $title = _draft_or_post_title( $draft->ID );
                    $user = get_userdata($draft->post_author);
                    $author = $user->display_name;
                    $item = \'<a href="\' . $url . \'" title="\' . sprintf( __( \'Edit “%s”\', FB_DF_TEXTDOMAIN ), esc_attr( $title ) ) . \'">\' . $title . \'</a> \' . __( \'by\', FB_DF_TEXTDOMAIN ) . \' \' . stripslashes( apply_filters(\'comment_author\', $author) ) . \' <abbr title="\' . get_the_time(__(\'Y/m/d g:i:s A\'), $draft) . \'">\' . get_the_time( get_option( \'date_format\' ), $draft ) . \'</abbr>\';
                    $list[] = $item;
                }
        ?>
            <ul>
                <li><?php echo join( "</li>\\n<li>", $list ); ?></li>
            </ul>
            <p class="textright"><a href="edit.php?post_status=draft" class="button"><?php _e(\'View all\', FB_DF_TEXTDOMAIN); ?></a></p>
        <?php
            } else {
                _e( \'There are no drafts at the moment\', FB_DF_TEXTDOMAIN );
            }
        }

        function my_wp_dashboard_setup() {

            wp_add_dashboard_widget( \'my_wp_dashboard_recent_drafts\', __( \'Recents Drafts\', FB_DF_TEXTDOMAIN ) . \' <small>\' . __( \'of all authors\', FB_DF_TEXTDOMAIN ) . \'</small>\', array(&$this, \'my_wp_dashboard_recent_drafts\') );
        }

        function add_my_css() {

            $echo  = \'\';
            $echo .= "\\n";
            $echo .= \'<style type="text/css">\'."\\n";
            $echo .= \'<!--\'."\\n";
            $echo .= \'#my_wp_dashboard_recent_drafts abbr {\' . "\\n";
            $echo .= \'font-family: "Lucida Grande", Verdana, Arial, "Bitstream Vera Sans", sans-serif;\' . "\\n";;
            $echo .= \'font-size: 11px;\' . "\\n";
            $echo .= \'color: #999;\' . "\\n";
            $echo .= \'margin-left: 3px;\' . "\\n";
            $echo .= \'}\'."\\n";
            $echo .= \'-->\'."\\n";
            $echo .= \'</style>\'."\\n";

            echo $echo;
        }

        // add feed via hook
        function add_draft_feed() {

            // set name for the feed
            // http://examble.com/?feed=draft
            add_feed( \'draft\', array(&$this, \'get_draft_feed\') );
        }

        // get feed
        function get_draft_feed() {
            global $wpdb;

            // draft or future
            $sql = "
                            SELECT ID, post_title, post_date, post_author, post_author, guid, post_excerpt, post_content
                            FROM $wpdb->posts
                            WHERE post_status = \'draft\'
                            ORDER BY post_date_gmt DESC
                        ";
            $items = $wpdb->get_results($sql);

            if ( !headers_sent() )
                header(\'Content-Type: text/xml; charset=\' . get_option(\'blog_charset\'), true);
            $more = 1;
        ?>
        <?php 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/"
    <?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>
    <pubDate><?php echo mysql2date( \'D, d M Y H:i:s +0000\', get_lastpostmodified(\'GMT\'), false ); ?></pubDate>
    <generator>http://bueltge.de/</generator>
    <language><?php echo get_option( \'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
    if ( empty($items) ) {
        echo \'<!-- No submissions found yet. //-->\';
    } else {
        foreach ($items as $item) {
    ?>
        <item>
            <title><?php echo stripslashes( apply_filters( \'comment_author\', $item->post_title ) ); ?></title>
            <link><?php echo stripslashes( apply_filters( \'comment_author_url\', get_permalink($item->ID) ) ); ?></link>
            <pubDate><?php echo mysql2date( \'D, d M Y H:i:s +0000\', $item->post_date ); ?></pubDate>
            <dc:creator><?php echo stripslashes( apply_filters(\'comment_author\', $item->post_author) ); ?></dc:creator>

            <guid isPermaLink="false"><?php echo stripslashes( apply_filters(\'comment_author_url\', $item->guid) ); ?></guid>
            <?php if ( $item->post_excerpt != \'\' ) { ?>
            <description><![CDATA[<?php echo trim(stripslashes( apply_filters(\'comment_text\', $item->post_excerpt) ) ); ?>]]></description>
            <?php } else { ?>
            <description><![CDATA[<?php echo strip_tags( trim( stripslashes( apply_filters(\'comment_text\', $item->post_content) ) ) ); ?>]]></description>
            <?php } ?>
            <content:encoded><![CDATA[<?php echo trim( stripslashes( apply_filters( \'comment_text\', $item->post_content ) ) ); ?>]]></content:encoded>
            <?php do_action(\'rss2_item\'); ?>
        </item>
    <?php
        }
    }
    ?>
    </channel>
</rss>
        <?php
        }

    } // end class

    $df_wp_injector = new DraftFeed();
} // end if class exists

// WP init and add ne function for feed
if ( isset($df_wp_injector) && function_exists( \'add_action\' ) ) {
    add_action( \'DraftFeed\',  array(&$df_wp_injector, \'init\') );
}
?>

SO网友:prettyboymp

您应该可以通过访问http://www.yoursite.com/?feed=rss2&post_type=your_post_type 只要帖子类型设置为public\\u queryable。

SO网友:Suraj Lulla

我相信您只需要添加一个带有自定义帖子类型(CPT)slug的查询字符串。

例如https://www.domain.com/feed/?post_type=your_post_type

结束