Created
April 20, 2012 19:09
-
-
Save staylor/2431117 to your computer and use it in GitHub Desktop.
Get the actual number of comments + replies per page, WP comments_per_page is really top-level comments only: allows you to identify 1-12 of 13, etc for comments count UI instead of mistakenly labeling page 1 as 1-10
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?php | |
function get_cpage_comment_count() { | |
$counter = $start = 0; | |
wp_list_comments( array( 'callback' => function ( $comment ) use ( &$counter, &$start ) { | |
global $wpdb; | |
if ( 0 === $counter ) { | |
$sql = $wpdb->prepare( | |
"SELECT COUNT(comment_ID) FROM {$wpdb->comments} | |
WHERE comment_approved = 1 AND | |
comment_post_ID = %d AND | |
comment_ID <> %d AND | |
((comment_ID > %d AND comment_parent <= %d AND comment_parent > 0) OR | |
(comment_ID < %d AND comment_parent <= %d))", | |
$comment->comment_post_ID, | |
$comment->comment_ID, | |
$comment->comment_ID, | |
$comment->comment_ID, | |
$comment->comment_ID, | |
$comment->comment_ID | |
); | |
$start = $wpdb->get_var( $sql ); | |
} | |
$counter++; | |
} ) ); | |
return compact( 'counter', 'start' ); | |
} | |
global $emusic_comment_pagination; | |
$emusic_comment_pagination = null; | |
function comment_pagination() { | |
global $emusic_comment_pagination; | |
if ( get_comment_pages_count() > 1 && get_option( 'page_comments' ) ) : | |
if ( $emusic_comment_pagination ) { | |
echo $emusic_comment_pagination; | |
return; | |
} | |
ob_start(); | |
$page = (int) max( 1, get_query_var( 'cpage' ) ); | |
$pages = (int) get_comment_pages_count(); | |
$counts = get_comment_count( get_the_ID() ); | |
$paged_counts = get_cpage_comment_count(); | |
?> | |
<div class="comment-pagination"> | |
<?php if ( 1 < $pages && $page !== $pages ): | |
?><div class="comment-next"><?php next_comments_link( ' ' ); ?></div> | |
<?php endif; | |
if ( 1 < $page ): | |
?><div class="comment-prev"><?php previous_comments_link( ' ' ) ?></div> | |
<?php endif ?> | |
<div class="comment-pages"><?php | |
printf( | |
'%d-%d of %d %s', | |
1 === $page ? 1 : ( $paged_counts['start'] + 1 ), | |
min( $counts['approved'], ( 1 === $page ? 0 : $paged_counts['start'] ) + $paged_counts['counter'] ), | |
$counts['approved'], | |
1 === $page ? ' View More ' : '' | |
) ?></div> | |
</div> | |
<?php | |
$emusic_comment_pagination = ob_get_clean(); | |
echo $emusic_comment_pagination; | |
endif; | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment