WordPress Comments Pagination Without a Plugin

WordPress Comments Pagination Without a Plugin

There is a possibility to create a pagination in WordPress comments without using any plugins. WordPress has a built-in function paginate_comments_links() for this, which is appeared in WordPress 2.7.

It works on the basis of the function for a pagination on archive pages (as you know, it also may work without a plugin), therefore very similar to it.

Code of the function:

function wp_comments_corenavi() {
   $pages = '';
   $max = get_comment_pages_count();
   $page = get_query_var('cpage');
   if (!$page) $page = 1;
   $a['current'] = $page;
   $a['echo'] = false;

   $total = 0; //1 - display the text "Page N of N", 0 - not display
   $a['mid_size'] = 3; //how many links to show on the left and right of the current
   $a['end_size'] = 1; //how many links to show in the beginning and end
   $a['prev_text'] = '« Previous'; //text of the "Previous page" link
   $a['next_text'] = 'Next »'; //text of the "Next page" link

   if ($max > 1) echo '<div class="commentNavigation">';
   if ($total == 1 && $max > 1) $pages = '<span class="pages">Page ' . $page . ' of ' . $max . '</span>'."\r\n";
   echo $pages . paginate_comments_links($a);
   if ($max > 1) echo '</div>';

You need to paste this code the functions.php file of your theme. As you see the function also contains just five options.

To show the comments pagination on your site, paste the following code in comments.php of your theme near the wp_list_comments() function:

<?php if(function_exists('wp_comments_corenavi')) wp_comments_corenavi(); ?>

HTML code of the function will looks like this:

<div class="commentNavigation">
	<span class="pages">Page 7 of 14</span>
	<a class="prev page-numbers" href="http://site.name/post/comment-page-6/#comments">&laquo; Previous</a>
	<a class='page-numbers' href='http://site.name/post/comment-page-1/#comments'>1</a>
	<span class="page-numbers dots">...</span>
	<a class='page-numbers' href='http://site.name/post/comment-page-4/#comments'>4</a>
	<a class='page-numbers' href='http://site.name/post/comment-page-5/#comments'>5</a>
	<a class='page-numbers' href='http://site.name/post/comment-page-6/#comments'>6</a>
	<span class='page-numbers current'>7</span>
	<a class='page-numbers' href='http://site.name/post/comment-page-8/#comments'>8</a>
	<a class='page-numbers' href='http://site.name/post/comment-page-9/#comments'>9</a>
	<a class='page-numbers' href='http://site.name/post/comment-page-10/#comments'>10</a>
	<span class="page-numbers dots">...</span>
	<a class='page-numbers' href='http://site.name/post/comment-page-14/#comments'>14</a>
	<a class="next page-numbers" href="http://site.name/post/comment-page-8/#comments">Next &raquo;</a>

And you can use a CSS classes for styling this navigation. That’s it.

One more thing. Don’t forget to activate the option “Break comments into pages” in WordPress admin area.

Cоmmеnts (8):
  1. 1


    What pagination class is used in the first example on the picture of this post?

    • 2

      It’s just thematic picture from Internet.

      To create a navigation design is necessary to write a number of styles.

  2. 3

    Thanks a lot of this tips :)

  3. 4

    you are the best thanks you :)

  4. 5
    affitto in Toscana said:

    Thanks for sharing this information. I need script for plugins instantly. I got it from your post..

  5. 6

    Hi, I use comment pagination in my blog. When I checked my blog in google webmaster tools, I found several duplicate title tag caused by comment pagination use. Is it possible to fix this problem (duplicate title tag) without unactivated the comment pagination? Thank you very much. Aniway, your article above is very useful.

    • 7

      Put the following code before the </title> tag:

      <?php if (get_query_var('paged')) { echo ' - Page '.get_query_var('paged'); } ?>

  6. 8
    Ahmad Awais said:

    Very nice and easy

Lеаvе а Соmmеnt

© 2009–2022 Dimox.net  •  Privacy Policy