Using WordPress ‘post_comment_meta_box()’ PHP function

The post_comment_meta_box() WordPress PHP function displays comments for a post.

Usage

To use the function, call it with the current post object as a parameter:

post_comment_meta_box($post);

Parameters

  • $post (WP_Post): Required. The current post object.

More information

See WordPress Developer Resources: post_comment_meta_box

Examples

Display comments on a custom post edit page

Create a custom post edit page in your theme or plugin, and display comments using the post_comment_meta_box() function.

// custom-post-edit.php
// Get the post object
$post = get_post($post_id);

// Display the comments for the post
post_comment_meta_box($post);

Add a custom comments meta box to the post editing screen

In your theme’s functions.php or a custom plugin file, add a custom comments meta box to the post editing screen.

add_action('add_meta_boxes', 'add_custom_comments_meta_box');
function add_custom_comments_meta_box() {
add_meta_box('custom_comments', 'Custom Comments', 'display_custom_comments_meta_box', 'post', 'normal', 'high');
}

function display_custom_comments_meta_box($post) {
post_comment_meta_box($post);
}

Display comments for a specific post in the front-end

Create a template file or a shortcode to display comments for a specific post in the front-end using the post_comment_meta_box() function.

// Get the post object
$post = get_post($post_id);

// Display the comments for the post
post_comment_meta_box($post);

Display comments for the current post in a loop

In your theme’s loop, display comments for each post using the post_comment_meta_box() function.

// The Loop
if (have_posts()) :
while (have_posts()) : the_post();

// Display the comments for the current post
post_comment_meta_box($post);

endwhile;
endif;

Display comments for a post with a specific ID

Use the post_comment_meta_box() function to display comments for a post with a specific ID.

// Get the post object for a specific ID
$post_id = 42; // Replace with the desired post ID
$post = get_post($post_id);

// Display the comments for the post
post_comment_meta_box($post);