以下是如何创建自定义提要:
首先在主题的目录中创建一个新文件,命名为your-custom-feed.php
把这个代码放进去
<?php
/**
* custom RSS feed.
*
* @package WordPress
*/
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 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 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>
<description><![CDATA[<?php the_excerpt_rss() ?>]]></description>
<?php if ( strlen( $post->post_content ) > 0 ) : ?>
<content:encoded><![CDATA[<?php the_content_feed(\'rss2\') ?>]]></content:encoded>
<?php else : ?>
<content:encoded><![CDATA[<?php the_excerpt_rss() ?>]]></content:encoded>
<?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>
然后添加一个简单的函数,使用
do_feed_$hook
//load feed template
function create_my_customfeed() {
load_template( TEMPLATEPATH . \'your-custom-feed.php\');
}
add_action(\'do_feed_mycustomfeed\', \'create_my_customfeed\', 10, 1);
现在当您访问
http://yoursite.com/?feed=mycustomfeed
无论您在WordPress admin中定义了什么,您都将获得一个全文提要。
Bonus
如果要为自定义提要创建重写规则,则url可以是:
http://yoursite.com/mycustomfeed.xml
以及http://yoursite.com/feed/mycustomfeed/
将此代码添加到函数中。php文件:
//创建feed-rewrite规则
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;
}