Post Pic

Creating user-defined RSS feeds in WordPress

RSS feeds are very useful, and indeed, very popular. WordPress creates RSS feeds by default, which is good in 99% of times. But how to create your own, custom RSS Feed? Just read this post to find out!

If you need a custom RSS feed, like for example, a feed indexing only somes categories + tags, or if you redirected all WordPress RSS feeds to Feedburner but still want to be able to get a category feed, the solution is to use a page template.
Simply paste the following code in a new file, save it under the name custom-feed.php and upload it on your theme directory.
Once done, simply write a new page in WordPress Dashboard (Don't type any text it in), and select custom-feed.php as a page template.

<?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>http://yoast.com/</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>joost@yoast.com</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>

Credits goes to Joost de Valk for this awesome recipe!

22 Responses

Jun 13 2009 04:42

Amazing. Is needed for special cases. Thanks a lot.

Jun 13 2009 17:49

Great tip. Everything is possible in this WordPress. BTW, I have never thought using templates for RSS.

Jun 13 2009 19:06

Nice tips.Thanks for sharing.I’ll try it

Jun 14 2009 16:01

Good job, thanks for sharing the php script.

Jun 16 2009 18:34

Well done well done.. Love this!

Jun 17 2009 22:45

Nice tip Joost, thanks for sharing.

Jul 07 2009 21:44

Ahahahah… this is what i need.
Thanks jean for this awesome tips :)

Sep 10 2009 20:41

how do i change this so it only get’s post in a certain category? for example if i only want to show post that are in my video category and not the whole site.

Trackbacks:

Leave a Comment

* Name, Email, Comment are Required