Build customized comment lists with paging using WP Comment Query
WP Comment Query is a class used by WordPress to query comments, as described in the previous article on this site.WordPress Comment Query Class WP Comment Query Reference Documentation I have introduced the detailed parameters of WP Comment Query class, using WP Comment Query class, we can realize a variety of comment query, below, let's take a look at how to use WP Comment Query class to build a customized comment list with paging function.
First, get the data needed for comment paging
First, we need to get the data needed for comment paging, including the total number of comments, the number of comments displayed on each page, and the current page number.
$user = wizhi_get_current_user();
// current page number, note that the query parameter here can't be paged, it will conflict with the article's paging parameter
$paged = isset( $_GET[ 'cmpage' ] ) ? $_GET[ 'cmpage' ] : 1;
// total number of pages
$total = get_user_comments_count( $user->ID );
// number of comments displayed per page
$number = 6;
$offset = ( $paged - 1 ) * $number;
// Parameters for the comment query class WP Comment Query
$args = [
'user_id' => $user->ID, 'number' => $he_ID.
'offset' => $offset,
];
// Comments query example
$comments_query = new WP_Comment_Query( $args );
$comments = $comments_query->comments;
// Currently fetched comments data
$current_count = count( $comments );
Show comments queried by WP Comment Query
That is, the content of the comments displayed inside the list of comments, there can be comments on the author's avatar, comments on the author's nicknames and other data, because as an example to use, here is no longer output the detailed data, only the output of the comments as a demonstration of the content.
if ( $comments ) {
foreach ( $comments as $comment ) {
echo $comment->comment_content;
}
}
Display paging links based on query data
Direct link to next page
If you don't need the numeric paging, you can just display the next page of links, and display the previous page of links in a similar way.
// If the number of comments currently fetched is equal to the number of comments shown per page, it's not the last page, show the next page link.
if ( $number == $current_count ) {
echo '<a class="button" href="?tab=reply&paged=' . ( $paged + 1 ) . '">next page</a>';
}
Show numeric pagination links
If you need to display previous and next page links, use the WordPress paginate_links function, which is described in the article in this site. WordPress Create User List with Paging Feature is used in the comment paging in a similar way.
// Get the current page
$current_page = max( 1, $paged );
$max_num_pages = intval( $total / $number ) + 1;
// Display paginated links
echo paginate_links( [
'base' => get_permalink( $pid ) . '%_%',
'format' => '?cmpage=%#%',
'current' => $current_page,
'total' => $max_num_pages, 'type' => 'list'
'type' => 'list«, 'prev_text' =>
'prev_text' => __( »' previous_page' ),
'next_text' => __( 'Next Page "' ),
'end_size' => 2,
'mid-size' => 3, .
] );
In fact, in WordPress, the realization of paging is very simple, in addition to WordPress, a variety of query classes have been very good support for the creation of paging data required parameters, as long as we respond to the parameters of the appropriate data can be, the next time you encounter a similar need, you can try your hand.