WordPress函数comment_type()用法解析(获取评论的类型)

文 / @WordPress主题

02345ef8033e7615a54e450911462b5aWordPress的comment_type函数用于获取评论的类型。WordPress允许用户发表两种类型的评论:普通评论和某些插件可能支持的自定义评论类型。comment_type函数可以用于区分这两种类型的评论,以便你可以对它们做出不同的处理。

例如,如果你想要在评论列表页面上区分普通评论和自定义评论类型,你可以在你的主题文件中添加如下代码:

// Get all comments for the current post

$comments = get_comments();
if ( ! empty( $comments ) ) {
foreach ( $comments as $comment ) {
// Get the comment type

$comment_type = comment_type( '', $comment->comment_ID );
// Check if the comment type is 'pingback' or 'trackback'
if ( in_array( $comment_type, array( 'pingback', 'trackback' ) ) ) {
// Display the comment type
echo '<p>' . $comment_type . '</p>';
} else {
// Get the comment text
$comment_text = comment_text( $comment->comment_ID );
// Display the comment text
echo '<p>' . $comment_text . '</p>';
}
}
}

在这段代码中,我们首先使用get_comments()函数来获取当前文章的所有评论。然后,我们使用foreach循环遍历每一条评论,并使用comment_type()函数来获取评论的类型。

接着,我们使用in_array函数来检查评论的类型是否为pingback或者trackback

添加UTHEME为好友
扫码添加UTHEME微信为好友
· 分享WordPress相关技术文章,主题上新与优惠动态早知道。
· 微信端最大WordPress社群,限时免费入群。