post_comment_meta_box()
概述
post_comment_meta_box() 是一个 WordPress 函数,用于在后台编辑文章页面显示评论管理界面。它生成一个包含评论列表和操作按钮的 meta box,支持查看、添加和删除评论。
关键要点
- 函数接受一个 WP_Post 对象作为必需参数,用于指定当前文章。
- 使用 wp_nonce_field() 添加安全 nonce 字段,防止 CSRF 攻击。
- 调用 get_comments() 获取文章评论数量,并利用 WP_Post_Comments_List_Table 类显示评论列表。
- 根据评论数量动态显示“暂无评论”或“查看所有评论”链接。
- 函数在 WordPress 2.8.0 版本中引入,位于 wp-admin/includes/post.php 文件中。
代码示例
function post_comment_meta_box( $post ) {
wp_nonce_field( 'get-comments', 'add_comment_nonce', false );
?>
<p id="add-new-comment"><a href="#commentstatusdiv" onclick="commentReply.open('add',<?php echo $post->ID; ?>);" class="button" aria-label="Add new comment">Add new comment</a></p>
<?php
$total = get_comments(
array(
'post_id' => $post->ID,
'count' => true,
'orderby' => 'none',
)
);
$wp_list_table = _get_list_table( 'WP_Post_Comments_List_Table' );
$wp_list_table->display( true );
if ( 1 > $total ) {
echo '<p id="no-comments">' . __( 'No comments yet.' ) . '</p>';
} else {
$hidden = get_hidden_meta_boxes( get_current_screen() );
if ( ! in_array( 'commentsdiv', $hidden, true ) ) {
?>
<p class="hide-if-no-js"><a href="#commentstatusdiv" onclick="commentsBox.load(<?php echo $post->ID; ?>);return false;" class="load-comments">View all comments</a> <span class="spinner"></span></p>
<?php
}
}
} Displays comments for post.
Parameters
$postWP_Postrequired-
Current post object.
Source
function post_comment_meta_box( $post ) {
wp_nonce_field( 'get-comments', 'add_comment_nonce', false );
?>
<p class="hide-if-no-js" id="add-new-comment"><button type="button" class="button" onclick="window.commentReply && commentReply.addcomment(<?php echo $post->ID; ?>);"></button></p>
$post->ID,
'count' => true,
'orderby' => 'none',
)
);
$wp_list_table = _get_list_table( 'WP_Post_Comments_List_Table' );
$wp_list_table->display( true );
if ( 1 > $total ) {
echo '<p id="no-comments">' . __( 'No comments yet.' ) . '</p>';
} else {
$hidden = get_hidden_meta_boxes( get_current_screen() );
if ( ! in_array( 'commentsdiv', $hidden, true ) ) {
?>
<script type="text/javascript"></script>
<p class="hide-if-no-js" id="show-comments"><a href="#commentstatusdiv" onclick="commentsBox.load(<?php echo $total; ?>);return false;"></a> <span class="spinner"></span></p>
</pre><p class="wporg-dot-link-list"><a href="https://developer.wordpress.org/reference/files/wp-admin/includes/meta-boxes.php/">View all references</a> <a href="https://core.trac.wordpress.org/browser/tags/6.9.4/src/wp-admin/includes/meta-boxes.php#L895">View on Trac</a> <a href="https://github.com/WordPress/wordpress-develop/blob/6.9.4/src/wp-admin/includes/meta-boxes.php#L895-L927">View on GitHub</a></p></section>
<section class="wp-block-wporg-code-reference-related" data-nosnippet="true"><h2 id="related" class="is-toc-heading wp-block-heading has-heading-5-font-size" tabindex="-1" ><a href="#related">Related</a></h2> <section style="margin-top:var(--wp--preset--spacing--20)" class="wp-block-wporg-code-table" id="uses"><figure class="wp-block-table "><table><thead><tr><th scope="col">Uses</th><th scope="col">Description</th></tr></thead><tbody><tr class=""><td><a href="https://developer.wordpress.org/reference/functions/get_current_screen/">get_current_screen()</a><code>wp-admin/includes/screen.php
Get the current screen object
get_hidden_meta_boxes()wp-admin/includes/screen.php
Gets an array of IDs of hidden meta boxes.
WP_List_Table::display()wp-admin/includes/class-wp-list-table.php
Displays the table.
_get_list_table()wp-admin/includes/list-table.php
Fetches an instance of a WP_List_Table class.
wp_comment_trashnotice()wp-admin/includes/template.php
Outputs ‘undo move to Trash’ text for comments.
wp_nonce_field()wp-includes/functions.php
Retrieves or display nonce hidden field for forms.
get_comments()wp-includes/comment.php
Retrieves a list of comments.
_e()wp-includes/l10n.php
Displays translated text.
__()wp-includes/l10n.php
Retrieves the translation of $text.
Changelog
| Version | Description |
|---|---|
| 2.8.0 | Introduced. |