Using WordPress ‘comment_type()’ PHP function

The comment_type() WordPress PHP function displays the type of the current comment on a WordPress site. It is typically used within the comment loop to display the type of each comment.

Usage

comment_type( $commenttxt, $trackbacktxt, $pingbacktxt );

The function will print out the type of comment – whether it is a regular comment, a trackback, or a pingback. If specific text is provided for each type, that text will be displayed instead.

Parameters

  • $commenttxt (string|false): Optional. String to display for comment type. Default: false.
  • $trackbacktxt (string|false): Optional. String to display for trackback type. Default: false.
  • $pingbacktxt (string|false): Optional. String to display for pingback type. Default: false.

More information

See WordPress Developer Resources: comment_type()
The function has been part of WordPress since version 1.5.

Examples

Basic Usage

This example displays the comment type of the current comment.

// Inside the comments loop
comment_type();

This will output ‘Comment’, ‘Trackback’, or ‘Pingback’ according to the type of the comment.

Custom Comment Type Texts

This example provides custom text for each type of comment.

// Inside the comments loop
comment_type('Custom Comment Text', 'Custom Trackback Text', 'Custom Pingback Text');

This will output the custom texts specified for each type of comment.

Using with a Conditional

In this example, we only want to display the comment type if it’s not a standard comment.

// Inside the comments loop
if (get_comment_type() !== 'comment') {
    comment_type();
}

This will only output ‘Trackback’ or ‘Pingback’, and will not output anything for regular comments.

Custom Text for Only One Type

Here, we only provide custom text for trackbacks.

// Inside the comments loop
comment_type(false, 'This is a trackback!', false);

This will output ‘This is a trackback!’ for trackbacks, and ‘Comment’ or ‘Pingback’ for the other types.

Displaying Comment Type in a Sentence

This example will incorporate the comment type into a larger piece of text.

// Inside the comments loop
echo 'This is a ';
comment_type();
echo '.';

This will output ‘This is a Comment.’, ‘This is a Trackback.’, or ‘This is a Pingback.’ depending on the comment type.