How to get the current post ID in comments.php?

admin2025-04-21  0

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!

Share Improve this question edited Aug 29, 2019 at 13:53 fuxia 107k39 gold badges255 silver badges459 bronze badges asked Aug 29, 2019 at 13:32 bilal BOUASRIAbilal BOUASRIA 131 silver badge5 bronze badges 1
  • 1 cause i dont want the regular HTML that wp_list_comments() provides i want my own HTML that i have olready writen – bilal BOUASRIA Commented Aug 29, 2019 at 21:03
Add a comment  | 

1 Answer 1

Reset to default 0

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'
    );
转载请注明原文地址:http://conceptsofalgorithm.com/Algorithm/1745201284a290067.html

最新回复(0)