The Tuts+ Guide to Template Tags: Fourth Batch

In the fourth part of the series, we went through the third batch of WordPress template tags. In this fifth part, we're going to go through the fourth batch of the almost 200 template tags. Throughout the tutorial, we'll see template tags about comments again, just like the previous batch.

Getting & Displaying a Link to the "Comments" Section: get_comments_link() & comments_link()

These template tags return and display the link to the post's "Comments" section.

Parameters

comments_link() doesn't accept any parameters, but get_comments_link() accepts one:

  • $post_ID (optional—integer or object):
    The ID of the post.
    (Default: Current post)

Usage

Getting & Displaying the List of Comments: wp_list_comments()

This popular template tag lists comments submitted on a post.

Parameters

This template tag accepts two parameters:

  • $args (optional—array):
    An array of the following arguments:
    • 'walker' (object): Instance of a Walker class to list comments.
      (Default: NULL)
    • 'max_depth' (integer): The maximum comments depth.
      (Default: Empty)
    • 'style' (string): The style of list ordering ('ul' or 'ol').
      (Default: 'ul')
    • 'callback' (string): Callback function to use.
      (Default: NULL)
    • 'end-callback' (string): Callback function to use at the end.
      (Default: NULL)
    • 'type' (string): Type of comments to list ('all, 'comment', 'pingback', 'trackback' or 'pings').
      (Default: 'all')
    • 'page' (integer): Page ID to list comments for.
      (Default: Empty)
    • 'per_page' (integer): Number of comments to list per page.
      (Default: Empty)
    • 'avatar_size' (integer): Dimensions of avatar pictures.
      (Default: 32)
    • 'reverse_top_level' (string): Ordering of the listed comments ('desc' or 'asc').
      (Default: NULL)
    • 'reverse_children' (boolean): Whether to reverse child comments in the list.
      (Default: Empty)
    • 'format' (string): How to format the comments list.
      (Default: 'html5' if the theme supports HTML5 markup, else 'xhtml')
    • 'short_ping' (boolean): Whether to output short pings.
      (Default: FALSE)
    • 'echo' (boolean): Whether to echo the output (TRUE) or return it (FALSE).
      (Default: TRUE)
  • $comments (optional—array):
    Array of comment objects.
    (Default: All comments for the current post)

Usage

Getting & Displaying the Number of Comments: get_comments_number() & comments_number()

These template tags get and display how many comments are posted to the post.

Parameters

get_comments_number() accepts only one parameter:

  • $post_ID (optional—integer or object):
    The ID of the post.
    (Default: Current post)

And comments_number() accepts three parameters:

  • $zero (optional—string):
    Text to display if there are no comments.
    (Default: 'No Comments')
  • $one (optional—string):
    Text to display if there is only one comment.
    (Default: '1 Comment')
  • $more (optional—string):
    Text to display if there is more than one comment.
    (Default: '% Comments')

Usage

Getting & Displaying Automatic Classes for Each Comment: get_comment_class() & comment_class()

These template tags let you add generated semantic classes to wherever you want in the comment loop.

Parameters

get_comment_class() accepts three parameters:

  • $class (optional—string or array):
    Extra class names to add.
    (Default: Empty)
  • $comment_ID (optional—integer):
    The ID of the comment to work with.
    (Default: Current comment's ID)
  • $post_ID (optional—integer or object):
    The ID of the post.
    (Default: Current post)

And comment_class() accepts four parameters:

  • $class (optional—string or array):
    Extra class names to add.
    (Default: Empty)
  • $comment_ID (optional—integer):
    The ID of the comment to work with.
    (Default: Current comment's ID)
  • $post_ID (optional—integer or object):
    The ID of the post.
    (Default: Current post)
  • $echo (optional—boolean):
    Whether echo (TRUE) or return (FALSE) the tag.
    (Default: TRUE)

Usage

Displaying the Comment Form: comment_form()

This template tag displays the whole comment form.

Parameters

This template tag accepts two parameters:

  • $args (optional—array):
    An array of the following arguments:
    • 'fields' (array): An associative array of default comment fields' HTML codes ('author', 'email' and 'url').
      (Default: All three of them)
    • 'comment_field' (string): HTML code for the comment's TEXTAREA element.
    • 'must_log_in' (string): HTML code for the "must log in to comment" message.
    • 'logged_in_as' (string): HTML code for the "logged in as..." message. (Use %1$s for the profile URL, %2$s for the username and %3$s for the logout URL.)
    • 'comment_notes_before' (string): HTML code for the notes shown before the comment form.
    • 'comment_notes_after' (string): HTML code for the notes shown after the comment form.
    • 'id_form' (string): The ID of the comment form's FORM element.
    • 'id_submit' (string): The ID of the submit button.
    • 'name_submit' (string): The NAME parameter of the submit button.
    • 'title_reply' (string): Label for the "Leave a Reply" button.
    • 'title_reply_to' (string): Label for the "Leave a Reply to %s" button.
    • 'cancel_reply_link' (string): Text of the "Cancel Reply" link.
    • 'label_submit' (string): Label for the "Post Comment" button.
    • 'format' (string): The comment form format ('xhtml' or 'html5', default is 'xhtml').
  • $post_ID (optional—integer or object):
    The ID of the post.
    (Default: Current post)

Usage

Displaying a Title for the Comment Form: comment_form_title()

This template tag echoes a dynamic title for the comment form.

Parameters

This template tag accepts three parameters:

  • $noreplytext (optional—string):
    Text of the link if it's not a "reply to comment" link.
    (Default: 'Leave a Reply')
  • $replytext (optional—string):
    Text of the link if it's a "reply to comment" link.
    (Default: 'Leave a Reply to %s')
  • $linktoparent (optional—boolean):
    Whether to link the commenter's name to his/her comment or not.
    (Default: TRUE)

Usage

Getting & Displaying the "Reply to Comment" Link: get_comment_reply_link() & comment_reply_link()

These template tags let you return or display a reply link for the given comment.

Parameters

Both template tags accept three parameters:

  • $args (optional—array):
    An array of the following arguments:
    • 'add_below' (string): The first part of the selector used to identify the comment to respond below. The resulting value is passed as the first parameter to addComment.moveForm(), concatenated as $add_below-$comment->comment_ID.
      (Default: 'comment')
    • 'respond_id' (string): The selector identifying the responding comment. Passed as the third parameter to addComment.moveForm(), and appended to the link URL as a hash value.
      (Default: 'respond')
    • 'reply_text' (string): Text of the 'Reply' link.
      (Default: 'Reply')
    • 'login_text' (string): Text of the link to reply if logged out.
      (Default: 'Log in to Reply')
    • 'depth' (integer): Depth of the new comment—must be greater than 0 and less than the value of the 'thread_comments_depth' option set in Settings > Discussion.
      (Default: 0)
    • 'before' (string): HTML code to add before the output.
      (Default: Empty)
    • 'after' (string): HTML code to add after the output.
      (Default: Empty)
  • $comment_ID (optional—integer):
    The ID of the comment to work with.
    (Default: Current comment's ID)
  • $post_ID (optional—integer or object):
    The ID of the post.
    (Default: Current post)

Usage

Getting & Displaying the Hidden Fields of the "Reply to Comment" Form: get_comment_id_fields() & comment_id_fields()

These template tags echo or return the hidden and mandatory input fields of a "reply to comment" link.

Parameters

Both template tags accept only one parameter:

  • $post_ID (optional—integer):
    The ID of the post.
    (Default: Current post's ID)

Usage

Getting & Displaying the "Cancel Reply" Link: get_cancel_comment_reply_link() & cancel_comment_reply_link()

These template tags return or echo a "Cancel Reply" link for using with the comment reply form.

Parameters

Both template tags accept only one parameter:

  • $text (optional—string):
    Text to display as the "cancel reply" link.
    (Default: "Click here to cancel reply.")

Usage

Getting & Displaying the Link for "Next Comments": get_next_comments_link() & next_comments_link()

These template tags return or print the "next comments" link in your theme's Comments section.

Parameters

Both template tags accept two parameters:

  • $label (optional—string):
    Text to display for the link.
    (Default: 'Newer Comments »')
  • $max_page (optional—integer):
    Maximum page number.
    (Default: 0)

Usage

Getting & Displaying the Link for "Previous Comments": get_previous_comments_link() & previous_comments_link()

These template tags return or print the "previous comments" link in your theme's Comments section.

Parameters

Both template tags accept only one parameter:

  • $label (optional—string):
    Text to display for the link.
    (Default: '« Older Comments')

Usage

Getting Pagination Links for the Comments: paginate_comments_links()

This template tag returns the pagination links for your theme's Comments section.

Parameters

This template tag accepts only one parameter:

  • $args (optional—array):
    An array of the following arguments:
    • 'base' (string): Base URL to be used to create paginated links.
    • 'format' (string): The argument to use for replacing the page number.
    • 'total' (integer): Total number of pages.
    • 'current' (integer): Current page number.
    • 'echo' (boolean): Whether to echo the template tag or not.
      (Default: TRUE)
    • 'add_fragment' (string): Text to prepend the page URLs.
      (Default: '#comments')

Usage

Getting & Displaying the "Edit Comment" Link: get_edit_comment_link() & edit_comment_link()

These template tags return or echo a "edit this comment" link for you to use in your templates.

Parameters

get_edit_comment_link() accepts one parameter:

  • $comment_ID (optional—integer):
    The ID of the comment to work with.
    (Default: current comment's ID)

And edit_comment_link() accepts three parameters:

  • $text (optional—string):
    Text to display the link.
    (Default: "Edit This")
  • $before (optional—string):
    The text or HTML code to display before the output.
    (Default: Empty)
  • $after (optional—string):
    The text or HTML code to display after the output.
    (Default: Empty)

Usage

Getting & Displaying the "Comments Feed" Link: get_post_comments_feed_link() & post_comments_feed_link()

These template tags return or display a link for the comments feed.

Parameters

get_post_comments_feed_link() accepts two parameters:

  • $post_ID (optional—integer):
    The ID of the post.
    (Default: Current post)
  • $feed (optional—string):
    Type of the feed.
    (Default: Default feed type)

And post_comments_feed_link() accepts three parameters:

  • $link_text (optional—string):
    Text to display for the link.
    (Default: "Comments Feed")
  • $post_ID (optional—integer):
    The ID of the post.
    (Default: Current post)
  • $feed (optional—string):
    Type of the feed.
    (Default: Default feed type)

Usage

Displaying a Link of the "Comments" Popup: comments_popup_link()

This template tag displays a link of the "Comments" popup, in case you want to use popup links in post lists.

Parameters

This template tag accepts five parameters:

  • $zero (optional—string):
    Text to display if there are no comments.
    (Default: 'No Comments')
  • $one (optional—string):
    Text to display if there is only one comment.
    (Default: '1 Comment')
  • $more (optional—string):
    Text to display if there is more than one comment.
    (Default: '% Comments')
  • $css_class (optional—string):
    Extra CSS class names to add.
    (Default: Empty)
  • $none (optional—string):
    Text to display if comments are turned off.
    (Default: 'Comments Off')

Usage

Displaying the "Comments" Popup Script: comments_popup_script()

This template tag builds and prints the script of the "Comments" popup.

Parameters

This template tag accepts three parameters:

  • $width (optional—integer):
    Width of the popup window.
    (Default: 400)
  • $height (optional—integer):
    Height of the popup window.
    (Default: 400)
  • $file (optional—string):
    URL to display on the address bar of the popup.
    (Default: Home URL)

Usage

Conclusion

Hope you liked this fourth batch of template tags. There are four more batches to go, so stay tuned for more template tags!

If you have any questions, comments or corrections, you can share your thoughts with us in the Comments section. And if you liked the article, don't forget to share it with your friends!

Tags:

Comments

Related Articles