How to truncate your WordPress RSS Feed at the More Tag

The More tag is a marker you can place in your WordPress posts that breaks up the post into two sections. It’s used to manually determine where the ‘read more’ link is placed when presenting a teaser of a post. WordPress doesn’t respect the More tag in RSS feeds by default, so if your rss feed is set to full or summary, it won’t break your posts as specified by the More tag. I have tried plugins that remedy this, but they fell short so I created my own function.

Fortunately, you can alter the content of RSS content through filters. What we want to have happen is to have a post break at the More tag and set the display of a teaser link (such as “continue reading” or “read more”). The two filters that are relevant are the_content_feed and the_excerpt_rss. These filters are applied before the RSS feed is generated and after regular content filters have been applied.

If your RSS feed is set to full and you want to break the posts up at the more tag position, here is an example function you can modify and place in your ‘functions.php’ file.

[php]
/*
This functions display a custom teaser, by finding the teaser content using preg_split. It then appends a custom more link and returns the filtered content which then will show up in your RSS feed.
*/
function dear_abby_rss_teaser( $content ){
$teaser = preg_split( ‘/<\/span>/’, $content );
$readmore = ‘Read Abby\’s answer here‘;
$content = $teaser[0].$readmore;
return $content;
}
add_filter( ‘the_content_feed’ ,’dear_abby_rss_teaser’ );
[/php]

8 thoughts on “How to truncate your WordPress RSS Feed at the More Tag

  1. Hi Peter, I know you’re not in the business of providing tech support, but when I add the code to my functions.php file, my site spits out this error:

    Parse error: syntax error, unexpected T_STRING in /nfs/c03/h04/mnt/168695/domains/ayearofproductivity.com/html/wp-includes/functions.php on line 4053

    Do you know how I might be able to fix this?

    Cheers!
    Chris

    1. Nice website Chris. Looks like there is a typo (a missing character like ” or ; probably) somewhere in your code, what version of WordPress do you have installed? If you paste your code to modify your RSS feed in pastebin.com I’ll have a look.

      1. Thanks a lot for the help Peter! I inserted your code snippet at the bottom of my functions.php file (wp-includes/functions.php), and I’m running 3.6. Here’s the ending of functions.php: http://pastebin.com/5WUcd1PV (only very slightly modified).

        Cheers!
        Chris

        1. Hi Chris, I was wondering why the functions.php was so long. So I should have been a bit clearer in the article, but there can be multiple functions.php files throughout the codebase, the one that you can add code to is the one located in your theme folder (/wp-content/themes/your-theme/functions.php). People use that file typically, although it’s better to put a piece of code like this in a small plugin. Anyhow, you’ve added it in a core WordPress file which will cause problems (and you lose your edits on a WP upgrade). So – what I recommend is restoring this file (replace wp-includes/functions.php with a fresh copy) and using a plugin for your RSS code. Here’s a zip of how it looks as a small plugin – you can move this to your /wp-content/plugins/ directory, then activate it and check to see if you are still getting an error.

          1. Ah, that makes a lot of sense! I modified the first functions.php file I found, because I thought it was the only one. Just appended the code and restored the original file, and it looks like it’s working. Thanks a lot for the help!

  2. Hi Peter,
    Many thanks for this helpful code. I am after something different in that I would like to maintain the “classic” the_content_feed for a full blog content feed, and create a second feed that just goes up to the more tag. I think your code will achieve the second objective but not the first. Is there any way I can add something similar directly into my new customfeed.php file, say? Any thoughts? Thank you for your wisdom here!

Leave a Reply

Your email address will not be published. Required fields are marked *

Current day month ye@r *