Function and usage of get_comment_link() in wordpress


Answers ( 1 )


    The get_comment_link() function in WordPress is used to retrieve the link to a specific comment. Here's a detailed explanation of its usage:

    Function Prototype:

    get_comment_link( WP_Comment|int|null $comment = null, array $args = array() ): string

    Function Description:

    • Purpose: To obtain the URL or permalink to a specific comment on a post in WordPress.
    • Usage Scenario: This function is particularly useful when you want to create direct links to comments, perhaps for sharing or for navigation purposes within your site.


    1. $comment (WP_Comment|int|null, optional):

      • The comment for which to retrieve the link.
      • It can be a WP_Comment object, an integer representing the comment ID, or null.
      • Default: null (which implies the current comment in the loop).
    2. $args (array, optional):

      • An array of optional arguments to modify the default behavior.
      • Key arguments include:
        • type (string): Passed to get_page_of_comment(). Limits comments to a specific type.
        • page (int): The current page number of comments, for calculating pagination.
        • per_page (int): The number of comments per page, used for pagination.
        • max_depth (int): Used by get_page_of_comment(), especially in threaded comments.
        • cpage (int|string): Specifies the "comment-page" or "cpage" value for the comment.
      • Additional arguments are derived from get_page_of_comment() and may include:
        • type: Limit comments to 'comment', 'trackback', 'pingback', 'pings', or 'all'.
        • per_page: Number of comments per page, defaults to the 'comments_per_page' setting.
        • max_depth: If greater than 1, determines the page for the top-level parent comment.
      • Default: array() (empty array, meaning default values are used).

    Return Value:

    • Returns a string representing the permalink to the specified comment.

    Sample Usage:

    // Assuming you have a comment ID or a WP_Comment object
    $comment_id = 123; // Example comment ID
    $comment_link = get_comment_link($comment_id);
    // Or for the current comment in a loop
    $comment_link = get_comment_link();
    // With additional arguments
    $args = array(
        'type' => 'all',
        'page' => 2,
        'per_page' => 10,
        'max_depth' => 3
    $comment_link = get_comment_link($comment_id, $args);
    // Output the link
    echo 'Link to the comment: ' . $comment_link;


    • It's important to use this function within the context of a WordPress loop or ensure that the $comment variable is appropriately defined.
    • The function ties in with WordPress's comment pagination system and can be influenced by various settings and arguments, making it versatile for different use cases.

Leave an answer