如何在Comments.php中获取当前帖子ID?

时间:2019-08-29 作者:bilal BOUASRIA

我想在评论中检索当前帖子的评论。php文件。我有这个:

<?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";
}
当我在第4行中没有任何内容时:\'post_id\' => "", 我在博客上得到了所有的评论。如何仅获取该帖子的评论?我需要动态获取帖子id!

enter image description here

1 个回复
最合适的回答,由SO网友:Mike Baxter 整理而成

您应该能够使用global $post;. 那么,你的$args 如下所示:

$args = array(
        \'post_id\' => $post->ID,
        \'order\'   => \'ASC\'
    );