I want to retrieve the comments of the current post in the comments.php file. I have this:
<?php
if (comments_open()) {
$args = array(
'post_id' => "|what to put here ?|",
'order' => 'ASC'
);
$comments = get_comments($args);
?>
<div class="comments-area">
<h4><?php comments_number('0 Comments', '1 Comments', '% Comments'); ?> post id is <?php echo "$postid"; ?></h4>
<?php foreach ($comments as $comment): ?>
<ul class="comment-list">
<li class="single-comment justify-content-between d-flex">
<div class="user justify-content-between d-flex">
<div class="thumb">
<?php echo get_avatar( $comment->comment_author_email, 32 ); ?>
</div>
<div class="desc">
<h5><?php echo "$comment->comment_author"; ?></h5>
<p class="date"><?php echo "$comment->comment_date"; ?></p>
<p class="comment">
<?php echo "$comment->comment_content"; ?>
</p>
</div>
</div>
<div class="reply-btn">
<a href="" class="btn-reply text-uppercase">reply</a>
</div>
</li>
</ul>
<?php endforeach ?>
</div>
<?php comment_form();
}else{
echo "comments disabled";
}
When I put nothing in line 4: 'post_id' => "",
I get all the comments in the blog. How to get only the comments of that post?
I need to dynamically get the post id!
I want to retrieve the comments of the current post in the comments.php file. I have this:
<?php
if (comments_open()) {
$args = array(
'post_id' => "|what to put here ?|",
'order' => 'ASC'
);
$comments = get_comments($args);
?>
<div class="comments-area">
<h4><?php comments_number('0 Comments', '1 Comments', '% Comments'); ?> post id is <?php echo "$postid"; ?></h4>
<?php foreach ($comments as $comment): ?>
<ul class="comment-list">
<li class="single-comment justify-content-between d-flex">
<div class="user justify-content-between d-flex">
<div class="thumb">
<?php echo get_avatar( $comment->comment_author_email, 32 ); ?>
</div>
<div class="desc">
<h5><?php echo "$comment->comment_author"; ?></h5>
<p class="date"><?php echo "$comment->comment_date"; ?></p>
<p class="comment">
<?php echo "$comment->comment_content"; ?>
</p>
</div>
</div>
<div class="reply-btn">
<a href="" class="btn-reply text-uppercase">reply</a>
</div>
</li>
</ul>
<?php endforeach ?>
</div>
<?php comment_form();
}else{
echo "comments disabled";
}
When I put nothing in line 4: 'post_id' => "",
I get all the comments in the blog. How to get only the comments of that post?
I need to dynamically get the post id!
You should be able to start your function with global $post;
. Then, your $args
would look like this:
$args = array(
'post_id' => $post->ID,
'order' => 'ASC'
);