get_comment_link()获取一个留言发布链接

目录

描述

译文

检索所给评论的链接

原文

Retrieve the link to a given comment.

get_comment_link() 描述

用法

<?php get_comment_link$comment$args ?>

get_comment_link() 用法

参数

$comment

(object|string|integer) (可选) Comment to retrieve.

默认值: null

$args

(mixed) (可选) Optional arguments (see Default Arguments.)

默认值: array (see Default Arguments)

get_comment_link() 参数

返回值

(string) 

The permalink to the current comment

get_comment_link() 返回值

注意

  • 使用到: get_comment() to retrieve $comment.
  • 使用到 global: (unknown) $wp_rewrite
  • 使用到 global: (unknown) $in_comment_loop

get_comment_link() 注意

历史

添加于 版本: 1.5.0

get_comment_link() 历史

源文件

get_comment_link() 函数的代码位于 wp-includes/comment-template.php.

/* ----------------------------------
 * wordpress之魂 © http://wphun.com
 * ---------------------------------- */
/**
 * Retrieve the link to a given comment.
 *
 * @since 1.5.0
 *
 * @see get_page_of_comment()
 *
 * @global WP_Rewrite $wp_rewrite
 * @global bool       $in_comment_loop
 *
 * @param mixed $comment Comment to retrieve. Default current comment.
 * @param array $args    Optional. An array of arguments to override the defaults.
 * @return string The permalink to the given comment.
 */
function get_comment_link( $comment = null, $args = array() ) {
	global $wp_rewrite, $in_comment_loop;

	$comment = get_comment($comment);

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

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

	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;
	}

	if ( $args['per_page'] ) {
		if ( '' == $args['page'] )
			$args['page'] = ( !empty($in_comment_loop) ) ? get_query_var('cpage') : get_page_of_comment( $comment->comment_ID, $args );

		if ( $wp_rewrite->using_permalinks() )
			$link = user_trailingslashit( trailingslashit( get_permalink( $comment->comment_post_ID ) ) . $wp_rewrite->comments_pagination_base . '-' . $args['page'], 'comment' );
		else
			$link = add_query_arg( 'cpage', $args['page'], get_permalink( $comment->comment_post_ID ) );
	} else {
		$link = get_permalink( $comment->comment_post_ID );
	}

	$link = $link . '#comment-' . $comment->comment_ID;
	/**
	 * Filter the returned single comment permalink.
	 *
	 * @since 2.8.0
	 *
	 * @see get_page_of_comment()
	 *
	 * @param string $link    The comment permalink with '#comment-$id' appended.
	 * @param object $comment The current comment object.
	 * @param array  $args    An array of arguments to override the defaults.
	 */
	return apply_filters( 'get_comment_link', $link, $comment, $args );
}

get_comment_link() 源文件