In the third part of the series, we went through the second batch of WordPress template tags. In this fourth part, we're going to go through the third batch of the almost 200 template tags. Throughout the tutorial, we'll see template tags about comments.
Getting & Displaying the Comment Author's Name: get_comment_author()
& comment_author()
These template tags return or display the name of the commenter.
Parameters
Both template tags accept only one parameter:
-
$comment_ID
(optional—integer):
The ID of the comment to work with.
(Default: current comment's ID)
Usage
<?php // Display the commenter's name. comment_author(); // Build an introduction of each comment. $comment_intro = get_comment_author() . __( ' says...', 'translation-domain' ); ?>
Getting & Displaying the Comment Author's Name for Feeds: get_comment_author_rss()
& comment_author_rss()
These template tags return or echo the comment author's name and makes it ready to display on feeds.
Parameters
These template tags don't accept any parameters.
Usage
<?php comment_author_rss(); ?>
Getting & Displaying the Comment Author's Email Address: get_comment_author_email()
& comment_author_email()
These template tags let you return or echo the email address of the commenter. (Warning: It's not cool to display commenters' email addresses in the front-end to the public, so make sure you're using it right.)
Parameters
Both template tags accept only one parameter:
-
$comment_ID
(optional—integer):
The ID of the comment to work with.
(Default: current comment's ID)
Usage
<?php // Get the email address of the commenter. comment_author_email(); // Return the email address of the commenter from a specific comment. $commenter_email = get_comment_author_email( 57 ); ?>
Getting & Displaying a Link of the Comment Author's Email Address: get_comment_author_email_link()
& comment_author_email_link()
These template tags let you return or echo the commenter's email address as a mailto:
link.
Parameters
Both template tags accept three parameters:
-
$linktext
(optional—string):
Text to display instead of the comment author's email address.
(Default: The email address) -
$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 // Get the email link of the commenter. comment_author_email_link(); // Return the email link of the commenter. $commenter_email_link = get_comment_author_email_link( '<i class="icon-email"></i>', __( 'Comment Author\'s Email Address', 'translation-domain' ), '<br />' ); ?>
Getting & Displaying the URL of the Commenter: get_comment_author_url()
& comment_author_url()
These template tags let you return or display the URL of the website of the comment author.
Parameters
Both template tags accept only one parameter:
-
$comment_ID
(optional—integer):
The ID of the comment to work with.
(Default: current comment's ID)
Usage
<?php // Display the comment author url. comment_author_url(); // Return the comment author url. $commenter_URL = get_comment_author_url(); // Return a link to the comment author's website from a specific comment. $commenter_link = '<a href="' . get_comment_author_url( 988 ) . '">' . __( 'Comment Author\'s Website', 'translation-domain' ) . '</a>'; ?>
Getting & Displaying the Link of the Commenter (With Author Name as Anchor Text): get_comment_author_link()
& comment_author_link()
These template tags get or echo the website link of the commenter with the commenter's name as the anchor text.
Parameters
Both template tags accept only one parameter:
-
$comment_ID
(optional—integer):
The ID of the comment to work with.
(Default: current comment's ID)
Usage
<?php // Display the comment author's link. comment_author_link(); // Return the comment author's link from a specific comment. $commenter_link = get_comment_author_link( 452 ); ?>
Getting & Displaying the Link of the Commenter (With Custom Text): get_comment_author_url_link()
& comment_author_url_link()
These template tags allow you to get or echo the link of the commenter's website with a custom text as the anchor.
Parameters
Both template tags accept three parameters:
-
$linktext
(optional—string):
Text to display.
(Default: The URL) -
$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 // Display a customized "commenter's website" link. comment_author_url_link( __( 'Comment author\'s website', 'translation-domain' ) ); // Return a customized "commenter's website" link with $before and $after. $comment_author_website = get_comment_author_url_link( __( 'Comment author\'s website', 'translation-domain' ), '<span class="icon-website">', '</span>' ); ?>
Getting & Displaying the IP Address of the Commenter: get_comment_author_IP()
& comment_author_IP()
These template tags return or display the IP address of the comment author.
Parameters
Both template tags accept only one parameter:
-
$comment_ID
(optional—integer):
The ID of the comment to work with.
(Default: current comment's ID)
Usage
<?php // Display the comment author's IP. comment_author_IP(); // Display the comment author's IP from a specific comment. $commenter_IP = get_comment_author_IP( 41 ); ?>
Getting & Displaying the Content of the Comment: get_comment_text()
& comment_text()
These template tags get and display the comment's content.
Parameters
Both template tags accept only one parameter:
-
$comment_ID
(optional—integer):
The ID of the comment to work with.
(Default: Current comment's ID)
Usage
<?php // Display the current comment's content. comment_text(); // Get a specific comment's content. $comment_content = get_comment_text( 965 ); ?>
Displaying the Content of the Comment for Feeds: comment_text_rss()
This template tag takes the comment content and makes it ready to display in feeds.
Parameters
This template tag doesn't accept any parameters.
Usage
<?php comment_text_rss(); ?>
Getting & Displaying the Excerpt of the Comment: get_comment_excerpt()
& comment_excerpt()
These template tags take the comment's content and cut it to show an "excerpt" of it.
Parameters
Both template tags accept only one parameter:
-
$comment_ID
(optional—integer):
The ID of the comment to work with.
(Default: current comment's ID)
Usage
<?php // Echo the current comment's excerpt. comment_excerpt(); // Return a given comment's excerpt. $comment_excerpt = get_comment_excerpt( 355 ); ?>
Getting & Displaying the Date of the Comment: get_comment_date()
& comment_date()
These template tags echo or return the date when the comment was posted.
Parameters
Both template tags accept two parameters:
-
$date_format
(optional—string):
The format of the date.
(Default: Date format set in the General Options page) -
$comment_ID
(optional—integer):
The ID of the comment to work with.
(Default: Current comment's ID)
Usage
<?php // Display the current comment's date. comment_date(); // Get a specific comment's date with a special date format. $some_comment_date = get_comment_date( 'MM/DD/YYYY', 9812 ); ?>
Getting & Displaying the Time of the Comment: get_comment_time()
& comment_time()
These template tags return or echo the time the comment was posted.
Parameters
get_comment_time()
accepts three parameters:
-
$time_format
(optional—string):
The format of the time.
(Default: Time format set in the General Options page) -
$gmt
(optional—boolean):
Whether to use the GMT date.
(Default:FALSE
) -
$translate
(optional—boolean):
Whether to pass todate_i18n()
function to translate the date.
(Default:TRUE
)
And comment_time()
accepts only one parameter:
-
$time_format
(optional—string):
The format of the time.
(Default: Time format set in the General Options page)
Usage
<?php // Display the current comment's time. comment_time(); // Get a specific comment's time with a special time format. $some_comment_time = get_comment_time( 'H:i:s', 115 ); ?>
Getting & Displaying the ID of the Comment: get_comment_ID()
& comment_ID()
These template tags do a very simple job: They get the ID of the comment.
Parameters
These template tags don't accept any parameters.
Usage
<?php comment_ID(); ?>
Displaying the Type of the Comment: comment_type()
This template tag lets you display the type of the comment—a normal comment, a trackback, or a pingback.
Parameters
This template tag accepts three parameters:
-
$commenttxt
(optional—string):
Text to display for "comment" type.
(Default: 'Comment' ) -
$trackbacktxt
(optional—string):
Text to display for "trackback" type.
(Default: 'Trackback') -
$pingbacktxt
(optional—string):
Text to display for "pingback" type.
(Default: 'Pingback')
Usage
<?php // Display comment type with default texts. comment_type(); // Display comment type with custom texts. comment_type( __( 'Reaction', 'translation-domain' ), __( 'Trackback', 'translation-domain' ), __( 'Ping', 'translation-domain' ) ); ?>
Getting the Avatar of a User: get_avatar()
This template tag lets you get the "avatar" of a user.
Parameters
This template tag accepts four parameters:
-
$id_or_email
(required—string, integer or object):
A user ID, email address, or comment object.
(Default:NULL
) -
$size
(optional—integer):
The size of the avatar in pixels.
(Default: 96) -
$default
(optional—string):
URL of the custom "default image", if there's no avatar available.
(Default: Empty) -
$alt
(optional—string):
The alternative text (alt
parameter) for the image.
(Default:FALSE
)
Usage
<?php // Returns current comment author's avatar. $commenter_email = get_comment_author_email(); $commenter_avatar = get_avatar( $commenter_email, 512 ); ?>
Conclusion
Hope you liked this third batch of template tags. There are five 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!
Comments