Using WordPress ‘comments_link_feed()’ PHP function

The comments_link_feed() WordPress PHP function outputs the link to the comments for the current post in a way that is safe for XML.

Usage

This function is typically used in RSS feeds. Here’s a basic example:

comments_link_feed();

This function will output a link like this:

http://example.com/post-name/feed/

Parameters

This function does not take any parameters.

More information

See WordPress Developer Resources: comments_link_feed

The function was implemented in WordPress version 2.5. It is still in use and not deprecated. The source code can be found in wp-includes/link-template.php.

Examples

Display comments feed link in a post

This code will display the comments feed link in a post.

// Check if comments are open for the post
if (comments_open()) {
  // Print the comments feed link
  echo 'Comments Feed: ';
  comments_link_feed();
}

This code will add a comments feed link in an RSS feed.

// In the loop of your RSS feed
while(have_posts()) : the_post();
  the_title();
  the_content();
  echo 'Comments: ';
  comments_link_feed();
endwhile;

This code will display the comments feed link for each post in a list of posts.

// Start the Loop.
while ( have_posts() ) : the_post();
  the_title();
  echo 'Comments: ';
  comments_link_feed();
endwhile;

This code will display the comments feed link in a custom WordPress widget.

class My_Widget extends WP_Widget {
  function widget($args, $instance) {
    echo $args['before_widget'];
    if (comments_open()) {
      echo 'Comments Feed: ';
      comments_link_feed();
    }
    echo $args['after_widget'];
  }
}

This code will display the comments feed link in the footer of your WordPress site.

// In your footer.php file
if (comments_open()) {
  echo 'Comments Feed: ';
  comments_link_feed();
}