By default, WordPress automatically generates the RSS feed for your website. And most readers of your WordPress site use this RSS feed to consume the content you produce. So, its a good idea to modify the default RSS feed on your WordPress site to include some additional information, like ads.
Add Custom Text or Ad Before and After Each Post in WordPress RSS Feed
To add some custom text, banner ad code or Adsense code, before and/or after each post in WordPress RSS feed, add this code to the functions.php file of your WordPress theme.
if(is_feed()){
$content = 'text before content'.$content.'text after content';
}
return $content;
}
add_filter('the_excerpt_rss', 'agentwp_insertRss');
add_filter('the_content', 'agentwp_insertRss');
Make sure to replace the text or ad code before and after content in this code.
Add a Different Text or Ad To Each Post in WordPress RSS Feed
The code above will add a text/ad code to each post in the RSS feed of your WordPress website, but it will same for each post. What if you want to display a different ad or text to each post in the feed? It can be done by using WordPress custom fields. Add this code to the functions.php file of your theme.
global $wp_query;
$postid = $wp_query->post->ID;
$customRSScontent = get_post_meta($postid, 'customRSScontent', true);
if(is_feed()) {
if($customRSScontent !== '') {
$content = $content."<br /><br /><div>".$customRSScontent."</div>
";
}
else {
$content = $content;
}
}
return $content;
}
add_filter('the_excerpt_rss', 'agentwp_insertCustomRss');
add_filter('the_content', 'agentwp_insertCustomRss');
Now you can add a custom field with name customRSScontent
for each post on your site and then put the custom text or ad code in its value field. This code or text will then automatically display at the end of each post in your RSS feed.
You don’t have to add the customRSScontent custom field to each post on your website. You can use it to show ads or custom text only on selected posts. If a post doesn’t have this custom field, then it will be displayed as it is.