WordPress函数comment_author_rss()用法详解(在RSS feed中显示评论的作者姓名)

文 / @WordPress主题

f69031c9b2b29770b53ef01d5fa0d36e-1

WordPress的comment_author_rss函数用于获取评论作者的姓名,并将其格式化为可在RSS feed中使用的字符串。这个函数通常用于在RSS feed中显示评论的作者姓名

下面是一个示例,展示了如何使用comment_author_rss函数来在RSS feed中显示评论作者的姓名:

function custom_rss_comments_feed() {
global $wp_query;
if ( $wp_query->have_comments() ) :
while ( $wp_query->have_comments() ) :
$wp_query->the_comment();
// Get the comment author's name and format it for use in the RSS feed
$author_name = esc_html( get_comment_author_rss() );
// Display the comment author's name in the RSS feed
echo '<h4>Comment by ' . $author_name . '</h4>';
// Get the comment content and format it for use in the RSS feed
$comment_content = get_comment_text_rss();
// Display the comment content in the RSS feed
echo '<p>' . $comment_content . '</p>';
endwhile;
endif;
}

在这段代码中,我们首先使用global $wp_query语句将WordPress的全局查询对象设置为一个可供我们使用的变量。然后,我们使用$wp_query->have_comments()函数来检查当前文章是否有评论。如果有,我们会使用while循环来遍历每一条评论,并使用$wp_query->the_comment()函数来处理每条评论。

在循环内部,我们使用get_comment_author_rss()函数来获取评论作者的姓名,并使用esc_html()函数来将其格式化为可在HTML中安全使用的字符串。

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