函数文档

comment_form_title()

💡 云策文档标注

概述

comment_form_title() 函数用于根据评论回复状态显示文本,主要影响禁用 JavaScript 的用户。它通常用于 comment_form() 函数下方和评论表单之前。

关键要点

  • 函数根据是否回复评论显示不同文本,默认无回复时显示“Leave a Reply”,回复时显示“Leave a Reply to %s”。
  • 参数包括 $no_reply_text(无回复文本)、$reply_text(回复文本,可包含 %s 占位符)、$link_to_parent(是否将作者名链接到评论)和 $post(指定文章)。
  • 内部依赖全局变量 $comment,以确保模板标签能访问当前评论。
  • 函数行为受 $_GET['replytocom'] 影响,通过 _get_comment_reply_id() 获取回复 ID。

代码示例

function comment_form_title( $no_reply_text = false, $reply_text = false, $link_to_parent = true, $post = null ) {
	global $comment;

	if ( false === $no_reply_text ) {
		$no_reply_text = __( 'Leave a Reply' );
	}

	if ( false === $reply_text ) {
		/* translators: %s: Author of the comment being replied to. */
		$reply_text = __( 'Leave a Reply to %s' );
	}

	$post = get_post( $post );
	if ( ! $post ) {
		echo $no_reply_text;
		return;
	}

	$reply_to_id = _get_comment_reply_id( $post->ID );

	if ( 0 === $reply_to_id ) {
		echo $no_reply_text;
		return;
	}

	// Sets the global so that template tags can be used in the comment form.
	$comment = get_comment( $reply_to_id );

	if ( $link_to_parent ) {
		$comment_author = sprintf(
			'%2$s',
			get_comment_ID(),
			get_comment_author( $reply_to_id )
		);
	} else {
		$comment_author = get_comment_author( $reply_to_id );
	}

	printf( $reply_text, $comment_author );
}

注意事项

  • 此函数仅对禁用 JavaScript 或未加载 comment-reply.js 的页面有效。
  • 在 WordPress 6.2.0 版本中新增了 $post 参数,允许指定文章。
  • 使用时需确保全局 $comment 变量已设置,以便模板标签正常工作。

📄 原文内容

Displays text based on comment reply status.

Description

Only affects users with JavaScript disabled.

{@internal The $comment global must be present to allow template tags access to the current comment. See https://core.trac.wordpress.org/changeset/36512.}

Parameters

$no_reply_textstring|falseoptional
Text to display when not replying to a comment.

Default:false

$reply_textstring|falseoptional
Text to display when replying to a comment.
Accepts “%s” for the author of the comment being replied to.

Default:false

$link_to_parentbooloptional
Boolean to control making the author’s name a link to their comment.

Default:true

$postint|WP_Post|nulloptional
The post that the comment form is being displayed for.
Defaults to the current global post.

Default:null

More Information

  • This function affects users with Javascript disabled or pages without the comment-reply.js JavaScript loaded.
  • This function is normally used directly below <div id="respond"> and before the comment form.

Source

function comment_form_title( $no_reply_text = false, $reply_text = false, $link_to_parent = true, $post = null ) {
	global $comment;

	if ( false === $no_reply_text ) {
		$no_reply_text = __( 'Leave a Reply' );
	}

	if ( false === $reply_text ) {
		/* translators: %s: Author of the comment being replied to. */
		$reply_text = __( 'Leave a Reply to %s' );
	}

	$post = get_post( $post );
	if ( ! $post ) {
		echo $no_reply_text;
		return;
	}

	$reply_to_id = _get_comment_reply_id( $post->ID );

	if ( 0 === $reply_to_id ) {
		echo $no_reply_text;
		return;
	}

	// Sets the global so that template tags can be used in the comment form.
	$comment = get_comment( $reply_to_id );

	if ( $link_to_parent ) {
		$comment_author = sprintf(
			'<a href="#comment-%1$s">%2$s</a>',
			get_comment_ID(),
			get_comment_author( $reply_to_id )
		);
	} else {
		$comment_author = get_comment_author( $reply_to_id );
	}

	printf( $reply_text, $comment_author );
}

Changelog

Version Description
6.2.0 Added the $post parameter.
2.7.0 Introduced.

User Contributed Notes

You must log in before being able to contribute a note or feedback.