comment_author_url()
云策文档标注
概述
comment_author_url() 是一个 WordPress 模板标签,用于在评论循环中输出当前评论作者的 URL,但不自动添加链接。它接受一个可选参数来指定评论 ID 或 WP_Comment 对象。
关键要点
- 函数功能:显示评论作者的 URL,不链接,适用于评论模板中。
- 参数:$comment_id(可选),可以是整数评论 ID 或 WP_Comment 对象,默认为当前评论。
- 内部调用:通过 get_comment_author_url() 获取 URL,并使用 apply_filters() 应用 'comment_url' 过滤器。
- 版本变化:从 WordPress 4.4.0 开始,$comment_id 参数支持 WP_Comment 对象。
代码示例
// 显示当前评论作者的 URL,不链接
comment_author_url();
// 显示指定评论 ID 的作者的 URL
comment_author_url( 123 );
// 示例:将评论作者 URL 作为链接输出,使用作者名作为链接文本
echo '<a href="' . comment_author_url() . '">Visit ' . get_comment_author() . ''s site</a>';
// 检查评论作者是否有 URL
if ( get_comment_author() == get_comment_author_link() ) {
// 如果作者没有 URL,执行某些操作
}注意事项
- 此函数直接输出 URL 字符串,不自动添加 HTML 链接标签,需手动处理链接化。
- 使用 'comment_url' 过滤器可以修改输出的 URL,参数包括 URL 和评论 ID。
- 在评论循环外使用时,需提供有效的 $comment_id 参数以确保正确获取评论数据。
原文内容
Displays the URL of the author of the current comment, not linked.
Parameters
$comment_idint|WP_Commentoptional-
WP_Comment or the ID of the comment for which to print the author’s URL.
Default current comment.
Source
function comment_author_url( $comment_id = 0 ) {
$comment = get_comment( $comment_id );
$comment_author_url = get_comment_author_url( $comment );
/**
* Filters the comment author's URL for display.
*
* @since 1.2.0
* @since 4.1.0 The `$comment_id` parameter was added.
*
* @param string $comment_author_url The comment author's URL.
* @param string $comment_id The comment ID as a numeric string.
*/
echo apply_filters( 'comment_url', $comment_author_url, $comment->comment_ID );
}
Hooks
- apply_filters( ‘comment_url’, string $comment_author_url, string $comment_id )
-
Filters the comment author’s URL for display.
Changelog
| Version | Description |
|---|---|
| 4.4.0 | Added the ability for $comment_id to also accept a WP_Comment object. |
| 0.71 | Introduced. |
Skip to note 3 content
Steven Lin
Example migrated from Codex:
Displays comment author’s URL as a link, using comment author’s name as part of the link text.
<a href="<?php comment_author_url(); ?>">Visit 's site</a>Skip to note 4 content
Muhammad Ayoub
this code check if comment author has url or not.
if ( get_comment_author() == get_comment_author_link() ) { // do something }