WordPress custom fields can be used to modify the RSS content of the blog or website after which you can have an option to add a specific content just for your RSS readers in a specific post. Firstly, an example would demonstrate the usage of custom field to display specific text/object in your WordPress RSS Feed. This trick will allow you to show different text, advertisement, image, or anything else for each post. First open your functions.php and paste the following codes in the php tags:
1 2 3 4 5 6 7 8 9 10 11 12 |
function wpbeginner_postrss($content) { global $wp_query; $postid = $wp_query->post->ID; $coolcustom = get_post_meta($postid, 'coolcustom', true); if(is_feed()) { if($coolcustom !== '') { $content = $content."<br /><br /><div>".$coolcustom."</div>\n"; } else { $content = $content; } } return $content; } add_filter('the_excerpt_rss', 'wpbeginner_postrss'); add_filter('the_content', 'wpbeginner_postrss'); |
After this process create a custom field with the name of “coolcustom” and add your desired value in it. You can add Google adsense in there, or any other ads/images/text that you like.
Secondly, another example would demonstrate the addition of extra text to post titles in RSS. This comes very handy when you are running a blog and have guest posts / sponsored posts. By utilizing this technique, you can tell your RSS readers in the title whether a specific post is a sponsored post or a guest post. For example if your title was “Use WordPress With Flickr” and it was a sponsored post, then you can change it to “Sponsored Post: Use WordPress With Flickr“. Same if someone wrote a guest post etc.
1 2 3 4 5 6 7 8 9 10 11 |
function wpbeginner_titlerss($content) { global $wp_query; $postid = $wp_query->post->ID; $gpost = get_post_meta($postid, 'guest_post', true); $spost = get_post_meta($postid, 'sponsored_post', true); if($gpost !== '') { $content = 'Guest Post: '.$content; } elseif ($spost !== ''){ $content = 'Sponsored Post: '.$content; } else { $content = $content; } return $content; } add_filter('the_title_rss', 'wpbeginner_titlerss'); |
The code above searches for two custom fields’ name “guest_post” or “sponsored_post”. If any of these two custom fields are found with a value “true”, then it will add the appropriate text before the title. This technique can be utilized in various ways to fit whatever you like.