Using WordPress ‘comments_rss()’ PHP function

The comments_rss() WordPress PHP function returns a link to the post’s RSS feed. This function facilitates easy access to the comments of a particular post via an RSS feed.

Usage

To use comments_rss(), simply call the function like so:

echo comments_rss();

When you use this function in a WordPress loop, it will output the RSS feed link for the comments of the current post.

Parameters

  • There are no parameters for this function.

More Information

See WordPress Developer Resources: comments_rss()
This function was implemented in WordPress since version 1.5.0. Its source code can be found in wp-includes/link-template.php.

Related functions include get_post_comments_feed_link(), which provides the RSS feed link for a specific post.

Examples

Display RSS feed link in a post

This code will display the RSS feed link for comments of each post in your loop.

// Loop through posts
if (have_posts()) : 
    while (have_posts()) : the_post();

        // Echo the RSS feed link for post comments
        echo comments_rss();

    endwhile;
endif;

You can use the function to create a custom RSS link with your own text.

// Custom RSS link text
echo '<a href="' . comments_rss() . '">Subscribe to Comments RSS</a>';

Using in Single Post

This function can be used inside a single post to display the RSS link for that post.

if (is_single()) {
    echo comments_rss();
}

Checking if RSS feed exists

Before displaying the RSS feed link, you can check if it exists.

// Check if RSS feed exists
if (comments_rss()) {
    echo comments_rss();
} else {
    echo 'No Comments RSS Available';
}

Including in HTML

You can include the RSS feed link in your HTML code.

echo '<link rel="alternate" type="application/rss+xml" title="RSS 2.0" href="' . comments_rss() . '" />';