Portekoi
Portekoi

Reputation: 1157

How get comment pages count?

In my header theme file, i'm trying to get the number total of comments page.

I've try this :

echo get_comment_pages_count();
echo get_comment_pages_count(get_the_ID());

If i put this code in my "comments.php" theme page, it's working.

My purpose is for add a "noindex" tag in my header.

The most recents comments are on the max comments page. I don't want to see have a duplicate content with th others pages.

Example :

Page 1 => noindex
Page 2 => noindex
Page 3 => noindex
Page 4 => ok

In the header and function theme pages, this function return 0 : get_comment_pages_count();

Upvotes: 0

Views: 856

Answers (1)

Pete
Pete

Reputation: 954

As per WordPress Codex:

Typically you cannot use this function before the The Loop has started.

You can however, pull all comment objects into an array and use:

$comment_count = get_comment_pages_count($comments_array);

or you can query the comment count via $wpdb like so:

global $wpdb, $wp_query;
$comment_count = $wpdb->get_var("SELECT COUNT(*) FROM $wpdb->comments WHERE comment_post_ID = $wp_query->post->ID");
echo $comment_count;

Upvotes: 2

Related Questions