返回介绍

get_comment_link()

发布于 2017-09-10 23:07:18 字数 6860 浏览 1016 评论 0 收藏 0

get_comment_link( WP_Comment|int|null $comment = null,  array $args = array() )

Retrieve the link to a given comment.


description


参数

$comment

(WP_Comment|int|null) (Optional) Comment to retrieve. Default current comment.

Default value: null

$args

(array) (Optional) An array of optional arguments to override the defaults.

  • 'type'
    (string) Passed to get_page_of_comment().
  • 'page'
    (int) Current page of comments, for calculating comment pagination.
  • 'per_page'
    (int) Per-page value for comment pagination.
  • 'max_depth'
    (int) Passed to get_page_of_comment().
  • 'cpage'
    (int|string) Value to use for the comment's "comment-page" or "cpage" value. If provided, this value overrides any value calculated from $page and $per_page.

Default value: array()


返回值

(string) The permalink to the given comment.


源代码

File: wp-includes/comment-template.php

function get_comment_link( $comment = null, $args = array() ) {
	global $wp_rewrite, $in_comment_loop;

	$comment = get_comment($comment);

	// Back-compat.
	if ( ! is_array( $args ) ) {
		$args = array( 'page' => $args );
	}

	$defaults = array(
		'type'      => 'all',
		'page'      => '',
		'per_page'  => '',
		'max_depth' => '',
		'cpage'     => null,
	);
	$args = wp_parse_args( $args, $defaults );

	$link = get_permalink( $comment->comment_post_ID );

	// The 'cpage' param takes precedence.
	if ( ! is_null( $args['cpage'] ) ) {
		$cpage = $args['cpage'];

	// No 'cpage' is provided, so we calculate one.
	} else {
		if ( '' === $args['per_page'] && get_option( 'page_comments' ) ) {
			$args['per_page'] = get_option('comments_per_page');
		}

		if ( empty( $args['per_page'] ) ) {
			$args['per_page'] = 0;
			$args['page'] = 0;
		}

		$cpage = $args['page'];

		if ( '' == $cpage ) {
			if ( ! empty( $in_comment_loop ) ) {
				$cpage = get_query_var( 'cpage' );
			} else {
				// Requires a database hit, so we only do it when we can't figure out from context.
				$cpage = get_page_of_comment( $comment->comment_ID, $args );
			}
		}

		/*
		 * If the default page displays the oldest comments, the permalinks for comments on the default page
		 * do not need a 'cpage' query var.
		 */
		if ( 'oldest' === get_option( 'default_comments_page' ) && 1 === $cpage ) {
			$cpage = '';
		}
	}

	if ( $cpage && get_option( 'page_comments' ) ) {
		if ( $wp_rewrite->using_permalinks() ) {
			if ( $cpage ) {
				$link = trailingslashit( $link ) . $wp_rewrite->comments_pagination_base . '-' . $cpage;
			}

			$link = user_trailingslashit( $link, 'comment' );
		} elseif ( $cpage ) {
			$link = add_query_arg( 'cpage', $cpage, $link );
		}

	}

	if ( $wp_rewrite->using_permalinks() ) {
		$link = user_trailingslashit( $link, 'comment' );
	}

	$link = $link . '#comment-' . $comment->comment_ID;

	/**
	 * Filters the returned single comment permalink.
	 *
	 * @since 2.8.0
	 * @since 4.4.0 Added the `$cpage` parameter.
	 *
	 * @see get_page_of_comment()
	 *
	 * @param string     $link    The comment permalink with '#comment-$id' appended.
	 * @param WP_Comment $comment The current comment object.
	 * @param array      $args    An array of arguments to override the defaults.
	 * @param int        $cpage   The calculated 'cpage' value.
	 */
	return apply_filters( 'get_comment_link', $link, $comment, $args, $cpage );
}

更新日志

Versiondescription
4.4.0Added the ability for $comment to also accept a WP_Comment object. Added $cpage argument.
1.5.0Introduced.

相关函数

Uses

  • wp-includes/formatting.php: trailingslashit()
  • wp-includes/query.php: get_query_var()
  • wp-includes/functions.php: wp_parse_args()
  • wp-includes/functions.php: add_query_arg()
  • wp-includes/link-template.php: get_permalink()
  • wp-includes/link-template.php: user_trailingslashit()
  • wp-includes/plugin.php: apply_filters()
  • wp-includes/option.php: get_option()
  • wp-includes/class-wp-rewrite.php: WP_Rewrite::using_permalinks()
  • wp-includes/comment-template.php: get_comment_link
  • wp-includes/comment.php: get_page_of_comment()
  • wp-includes/comment.php: get_comment()
  • Show 7 more uses Hide more uses

Used By

  • wp-includes/rest-api/endpoints/class-wp-rest-comments-controller.php: WP_REST_Comments_Controller::prepare_item_for_response()
  • wp-admin/includes/dashboard.php: _wp_dashboard_recent_comments_row()
  • wp-admin/includes/ajax-actions.php: _wp_ajax_delete_comment_response()
  • wp-admin/includes/class-wp-comments-list-table.php: WP_Comments_List_Table::column_date()
  • wp-admin/includes/class-wp-comments-list-table.php: WP_Comments_List_Table::column_comment()
  • wp-includes/pluggable.php: wp_notify_postauthor()
  • wp-includes/widgets/class-wp-widget-recent-comments.php: WP_Widget_Recent_Comments::widget()
  • wp-includes/feed.php: comment_link()
  • wp-includes/class-wp-xmlrpc-server.php: wp_xmlrpc_server::_prepare_comment()
  • wp-includes/class-walker-comment.php: Walker_Comment::comment()
  • wp-includes/class-walker-comment.php: Walker_Comment::html5_comment()
  • Show 6 more used by Hide more used by

User Contributed Notes

  1. Skip to note content You must log in to vote on the helpfulness of this noteVote results for this note: 0You must log in to vote on the helpfulness of this note Contributed by thelittlemercoder

    Example of Comment Link on the Date in Comments

    
    $posted_on = sprintf( __( 'Posted on %1$s at %2$s', 'textdomain' ), get_comment_date( 'F j, Y' ), get_comment_time( 'g:ia' ) );
    printf( '<a href="%1$s">%2$s</a>', esc_url( get_comment_link() ), $posted_on ); 
    

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。
列表为空,暂无数据
    我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
    原文