Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Comment Template: Add handling for the pagination inner blocks #36623

Closed
Closed
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
29 changes: 26 additions & 3 deletions packages/block-library/src/comment-template/index.php
Original file line number Diff line number Diff line change
Expand Up @@ -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 ) {
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Under what circumstances would get_query_var( 'cpage' ); be falsy here and why would that mean that we should return only the count of the comments? Asking out of ignorance here :)

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

If you go to http://localhost:8888/hello-world/ that is falsy. We get the comments count in order to calculate which should be the offset in order to show the last comments.

For example, if you have 14 comments with 2 comments per page, show the 2 last ones. If you have 13 comments with 2 comments per page, show only one on the last page.

This, to be honest, I could not get it to work yet 😕

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Whoops, I missed your comment earlier. I think I get it now, thanks 😊

$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 '';
Expand Down