Displaying the comments link on a WordPress post using comments_link

The comments_link function in WordPress is designed to generate a URL that leads to the comments section of a particular post or page. This function can be useful in various situations where a direct link to the comments section is required. For instance, it can be used to create a comments link in a post meta data section, or in a list of posts, so that users can quickly navigate to the comments section of a post.

When the comments_link function is called, it retrieves the URL of the comments section of the current post in the WordPress Loop. The function does not return the URL, but instead directly outputs it to the browser. This means that it should be used in a context where it is appropriate to output HTML directly.

The comments_link function does not include the number of comments in the link. If you need to display the number of comments along with the link, you should use the comments_popup_link function instead.

The comments_link function is part of the WordPress Comments API, which provides a set of functions and hooks for working with comments in WordPress. Other functions in the API include get_comments, wp_list_comments, and comment_form.

Parameters Accepted by the WordPress comments_link Function

The comments_link function in WordPress accepts two parameters. However, both of these parameters are deprecated and not currently in use. These parameters are:

  • $deprecated: This is a string parameter and is optional. Its default value is an empty string (”). As mentioned, this parameter is no longer in use.
  • $deprecated_2: Similar to the first parameter, this too is a string parameter, optional, and its default value is also an empty string (”). This parameter is also not in use.

Return Value of the WordPress comments_link Function

The comments_link function in WordPress does not return any value.

Examples

How to Display the Comment Link in a Post Loop

<?php 
if ( have_posts() ) {
 while ( have_posts() ) {
 the_post(); 
 echo '<p>';
 comments_link();
 echo '</p>';
 } 
}
?>

This code snippet will display the comment link for each post in the WordPress loop. The comments_link() function is used without any parameters as the parameters are deprecated and not used.

How to Display the Comment Link Only if Comments are Open

<?php 
if ( have_posts() ) {
 while ( have_posts() ) {
 the_post(); 
 if (comments_open()) {
 echo '<p>';
 comments_link();
 echo '</p>';
 }
 } 
}
?>

This code snippet will display the comment link for each post in the WordPress loop only if comments are open for that post. The comments_open() function is used to check if comments are open for the post before displaying the comment link with the comments_link() function.

Conclusion

The comments_link() function in WordPress is a built-in template tag that generates a URL for the comments pop-up window for the current post. This function is primarily used in creating links that direct users to the comments section of a post, enabling them to either view existing comments or add their own. By integrating this function into your WordPress theme, you can facilitate user engagement and interaction on your posts.

Related WordPress Functions