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.
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
<?php
comments_link();
?>
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
<?php
$args = array(
// Use an ordered list for comments.
'style' => 'ol',
// Get comments only.
'type' => 'comments',
// Custom number of comments per page.
'per_page' => 15,
// Reduce avatar size.
'avatar_size' => 16,
// Force use HTML5 markup.
'format' => 'html5',
// Return the output instead of echoing.
'echo' => 0
);
$comments_list = wp_list_comments( $args );
?>
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
<?php
// Display the number of comments of a specific post.
comments_number( 11 );
// Display the number of comments of the current post.
$comments_num = get_comments_number( '0', '1', '%' );
?>
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
<?php
// Display comment classes with an extra class.
comment_class( 'mytheme-comment' );
// Return a specific comment from a specific post.
$special_comment_classes = get_comment_class( '', 225, 98 );
?>
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
<?php
$args = array(
'fields' => array( 'author', 'email' ),
'must_log_in' => __( 'You have to be a logged-in user to be able to comment.', 'translation-domain' ),
'label_submit' => __( 'Shoot It', 'translation-domain' ),
'format' => 'html5'
);
comment_form( $args );
?>
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
<?php
comment_form_title( __( 'Shoot a Reply', 'translation-domain' ), __( 'Talk Back to', 'translation-domain') . ' %s', false );
?>
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
<?php
// Display the link with default configuration.
comment_reply_link();
// Get the link with some customization.
$args = array(
'reply_text' => __( 'Shout Back', 'translation-domain' ),
'login_text' => __( 'Log in to Shout Back', 'translation-domain' ),
'after' => '<hr class="comment-reply-after" />'
);
$comment_reply_link = get_comment_reply_link( $args );
?>
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
<?php
// Display the hidden fields.
comment_id_fields();
// Get the hidden fields of a custom post's "reply to comment" form.
$some_post_ID = get_the_ID_of_some_post( 'this-is-not-a-real-function-BTW' );
$reply_hidden_fields = get_comment_id_fields( $some_post_ID );
?>
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
<?php
// Display the link.
cancel_comment_reply_link();
// Retrieve the link.
$cancel_reply_link = get_cancel_comment_reply_link( __( 'Meh...', 'translation-domain' ) );
?>
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
<?php
// Get the "next comments" link with default values.
$next_comments_link = get_next_comments_link();
// Display the "next comments" link with a different label.
next_comments_link( __( 'Next', 'translation-domain' ) );
?>
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
<?php
// Get the "previous comments" link with default values.
$previous_comments_link = get_previous_comments_link();
// Display the "previous comments" link with a different label.
previous_comments_link( __( 'Previous', 'translation-domain' ) );
?>
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
<?php
// Display the pagination links with default arguments.
paginate_comments_links();
// Get the pagination links and remove the #comments parts from links.
$args = array(
'echo' => false,
'add_fragment' => ''
);
$comment_pagination = paginate_comments_links( $args );
?>
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
<?php
// Retrieve the current comment's "edit comment" link.
$edit_comment_link = get_edit_comment_link();
// Retrieve a specific comment's "edit comment" link.
$edit_comment_link = get_edit_comment_link( 133 );
// Displaying the current comment's "edit comment" link.
edit_comment_link( __( 'Edit', 'translation-domain' ), '<span class="edit-comment-link">', '</span>' );
?>
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
<?php
// Get the post's "comments" link for atom feeds.
$comments_link_for_feeds = get_post_comments_feed_link( $post->ID, 'atom' );
// Display the post's "comments" link for default feed.
post_comments_feed_link();
?>
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
<?php
comments_popup_link(
__( 'No Reactions', 'translation-domain' ),
__( 'Just One Reaction', 'translation-domain' ),
'% ' . __( 'Reactions', 'translation-domain' ),
'my-comments-popup-link',
__( 'You Can\'t React to This Post!', 'translation-domain' )
);
?>
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
<?php
comments_popup_script( 600, 300 );
?>
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!
by Barış Ünver via Tuts+ Code