diff --git a/packages/block-library/src/comment-template/index.php b/packages/block-library/src/comment-template/index.php index 2843c56a38adc1..5684374843d323 100644 --- a/packages/block-library/src/comment-template/index.php +++ b/packages/block-library/src/comment-template/index.php @@ -24,10 +24,33 @@ function render_block_core_comment_template( $attributes, $content, $block ) { return ''; } - $number = $block->context['queryPerPage']; + $per_page = $block->context['queryPerPage']; - // Get an array of comments for the current post. - $comments = get_approved_comments( $post_id, array( 'number' => $number ) ); + $page = (int) get_query_var( 'cpage' ); + + $comment_args = array( + 'number' => $per_page, + 'orderby' => 'comment_date_gmt', + 'order' => 'ASC', + 'status' => 'approve', + 'post_id' => $post_id, + 'offset' => 0, + 'parent' => 0, // Only show top-level comments. Needs to be updated with responses. + ); + + if ( $page ) { + $comment_args['offset'] = ( $page - 1 ) * $per_page; + } else { + $top_level_args = array( + 'post_id' => $post_id, + 'count' => true, + 'parent' => 0, + ); + $top_level_count = get_comments( $top_level_args ); + $comment_args['offset'] = ( ceil( $top_level_count / $per_page ) - 1 ) * $per_page; + } + + $comments = get_comments( $comment_args ); if ( count( $comments ) === 0 ) { return '';