我在试着Yoast\'s Custom RSS feeds 在add\\U feed功能中进行后期显示。
只需将Yoast的代码片段放入add\\u feed中,结果如下:
错误:feedname不是有效的源模板。
我尝试了两个重写函数,但都没有用。我会错过什么?
Yoast的功能未触及:
<?php
/*
Template Name: Custom Feed
*/
$numposts = 5;
function yoast_rss_date( $timestamp = null ) {
$timestamp = ($timestamp==null) ? time() : $timestamp;
echo date(DATE_RSS, $timestamp);
}
function yoast_rss_text_limit($string, $length, $replacer = \'...\') {
$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;
}
$posts = query_posts(\'showposts=\'.$numposts);
$lastpost = $numposts - 1;
header("Content-Type: application/rss+xml; charset=UTF-8");
echo \'<?xml version="1.0"?>\';
?><rss version="2.0">
<channel>
<title>Yoast E-mail Update</title>
<link><a class="linkclass" href="http://yoast.com/">http://yoast.com/</a></link>
<description>The latest blog posts from Yoast.com.</description>
<language>en-us</language>
<pubDate><?php yoast_rss_date( strtotime($ps[$lastpost]->post_date_gmt) ); ?></pubDate>
<lastBuildDate><?php yoast_rss_date( strtotime($ps[$lastpost]->post_date_gmt) ); ?></lastBuildDate>
<managingEditor><a class="linkclass" href="mailto:[email protected]">[email protected]</a></managingEditor>
<?php foreach ($posts as $post) { ?>
<item>
<title><?php echo get_the_title($post->ID); ?></title>
<link><?php echo get_permalink($post->ID); ?></link>
<description><?php echo \'<![CDATA[\'.yoast_rss_text_limit($post->post_content, 500).\'<br/><br/>Keep on reading: <a href="\'.get_permalink($post->ID).\'">\'.get_the_title($post->ID).\'</a>\'.\']]>\'; ?></description>
<pubDate><?php yoast_rss_date( strtotime($post->post_date_gmt) ); ?></pubDate>
<guid><?php echo get_permalink($post->ID); ?></guid>
</item>
<?php } ?>
</channel>
</rss>
Function one:
function myPlugin_add_feed( ) {
global $wp_rewrite;
add_feed(\'feedname\', \'my_feed\');
add_action(\'generate_rewrite_rules\', \'myPlugin_rewrite_rules\');
$wp_rewrite->flush_rules();
}
add_action(\'init\', \'myPlugin_add_feed\');
以及
function two:
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\');
最合适的回答,由SO网友:Otto 整理而成
你有add_feed(\'feedname\', \'my_feed\');
但是没有my\\u feed函数来实际生成feed输出。
创建一个my\\u feed函数,并让它调用模板来生成feed输出。像这样:
function my_feed() {
include \'path-to-that-template-file.php\';
}
add_feed(\'feedname\',\'my_feed\');
然后,通过重新保存永久链接设置,只重新生成永久链接一次。
此外,您根本不需要任何额外的重写废话。只需add\\u提要就足够了。WP处理其余部分,您的提要将位于/feed/feedname。