WordPress development principles, provide you a way to customize almost any feature provided by default. For instance, the feed link of a page, by default, displays the comments for that page. A client once had a different requirement, to display the page’s content in the page feed, and not the comments. The solution was quite easy to implement. We reckon, this simple trick we used, could benefit those of you, who had a similar requirement.
Let’s take an example to explain this.
Say you have a page ‘Most Visited Posts’, which lists the popular posts on your website. And let’s consider the page’s URL to be ‘http://example.com/most-visited-posts’. Now, certainly this page would not have any comments. Individual posts might have comments, but not this page.
If you were to go to the feed link of that page, i.e., ‘http://example.com/most-visited-posts/feed’, then you would see nothing (because there are no comments). Therefore, people who would want to subscribe to your ‘Most Visited Posts’ via the page feed, will not see any content at that link.
[banner-full-light title=”We provide Customized Features” icon=”fontawesome-cogs”] Maybe your requirement is more complicated than customizing page feed. We assure tailor-made solutions, with a 100% satisfaction guarantee.[/banner-full-light]
Displaying the Page Content in the Page Feed
To show the content in the Page’s feed, we just made a simple change. Here’s how.
The is_feed function is called to determine if the query made is for a feed. By default, the is_comment_feed variable is set to true, thus displaying the comments as feed.
We need to change this, by setting the value for is_comment_feed to false. And in our case, since the requirement is conditional, we need to set this only for the page ‘Most Visited Posts’. You need to add the following code in the functions.php (or in a plugin if you chose so).
//hook to change the rss content for pages
add_action(‘template_redirect’, ‘wdm_show_content_in_page_feed’);
function wdm_show_content_in_page_feed() {
if(is_feed() && is_page(‘most-visited-posts’)) {
global $wp_query;
$wp_query->is_comment_feed = false;
}
}
where ‘most-visited-posts’ is the slug for your ‘Most Visited Posts’ page.
The above action is triggered on the hook ‘template_redirect‘. The action checks if the current link is a feed link or not, and if current page is ‘most-visited-posts’ page. If both these conditions are matched, it will set is_comment_feed to false.
When is_comment_feed variable is false, the content in the page will be displayed.
This was a simple enough requirement, and we were quick to provide a solution. Nevertheless, we always focus on providing the most appropriate solution for each requirement.
Leave a Reply