Home > Web Design, WordPress > WordPress Wednesday: How to Use paginate_comments_links Only When You Really Mean It

WordPress Wednesday: How to Use paginate_comments_links Only When You Really Mean It

March 10, 2010

I was pretty happy when WordPress added paginate_comments_links because now I didn’t have to write a custom function to break blog comments into separate pages and give them a nice navigation (instead of just “next” and “previous”). However, I like to put my navigation in a div, and I like to style that div…and it can look pretty bad when my styled div shows up with no navigation (depending on my design).

The solution is pretty simple (as most of my solutions are): I wrote a simple function that only echoes my div and pagination links if the comments are paginated:

function comment_pagination() {
	//read the page links but do not echo
	$comment_page = paginate_comments_links('echo=0');

	//if there are page links, echo the navigation div and the page links
	if (!empty($comment_page)) {
		echo "<div class=\"navigation\">\n";
		echo $comment_page;
		echo "\n</div>\n";
		}
}

All that’s left at this point is styling the pagination links.  The above code will give you a markup along these lines:

<div class="navigation">
	<a class='prev page-numbers'>&laquo; Previous</a>
	<a class='page-numbers'>1</a>
	<span class='page-numbers current'>2</span>
	<a class='page-numbers'>3</a>
	<a class='next page-numbers'>Next &raquo;</a>
</div>

Hopefully I’ll learn more about paginate_comments_links in the future and be able to better customize the output. We’ll see.



Advertisements
%d bloggers like this: