Improved separation of comments and pingbacks in Thematic 1.0.1

In previous versions of Thematic, the code to separate pingbacks and trackbacks has been based on this article by Ian Stewart. In that article he laments that comments and trackbacks are displayed in the same list by default and shows how to separate them in the comments template. The article was written in 2009, but the principle is common. Use a counter variable to count the number of comments and trackbacks and then make two separate loops to display them. The problem lies in that the paginate_comments_link() function only gets called on the comments list. There are no pagination links for the trackbacks.

Say you have 8 comments and 15 trackbacks, and you have set WordPress to display a maximum of 5 comments per page. That would give you 2 comment-pages and 3 trackback-pages, which is 13 responses = 5 pages altogether. But since we are separating the trackbacks from the pingbacks and the pagination links only were for the comments, the pagination would only show 2 pages and any trackback-pages beyond that would not be reachable. And on top of that, WordPress keeps track of the number of comment-pages internally so that when someone adds a comment they would be redirected to comment-page number 5 – which would be completely empty!

All of this has been addressed in the release of Thematic 1.0.1. For those of you who want to know how it’s done…

Separating comments and trackbacks

Let’s start with looking at some code. This snippet comes from the beginning of the comments loop.

// Collect the comments and pings
$thematic_comments = $wp_query->comments_by_type['comment'];
$thematic_pings = $wp_query->comments_by_type['pings'];

// Calculate the total number of each
$thematic_comment_count = count( $thematic_comments );
$thematic_ping_count = count( $thematic_pings );

// Get the page count for each
$thematic_comment_pages = get_comment_pages_count( $thematic_comments );
$thematic_ping_pages = get_comment_pages_count( $thematic_pings );

// Determine which is the greater pagination number between the two (comment,ping) paginations
$thematic_max_response_pages = ( $thematic_ping_pages > $thematic_comment_pages ) ? $thematic_ping_pages : $thematic_comment_pages;

// Reset the query var to use our calculation for the maximum page (newest/oldest)
if ( $overridden_cpage )
set_query_var( 'cpage', 'newest' == get_option('default_comments_page') ? $thematic_comment_pages : 1 );

First of all, there’s no need to have a counter in the comment loop to see how many comments and trackbacks you have. We can grab them directly from the query and count them. Pings are both trackbacks and pingbacks, they will be in the same list.

The get_comment_pages_count() function will calculate the number of pages for us, taking into account both the threading of the comments and the user seleted number of comments per page. We save the largest page number in a variable for later use.

The last bit is there to make sure the correct comment page is shown based on whether you have selected to show the newest or oldest comments first in your discussion settings. The $overridden_cpage variable is a global set by WordPress that decides which comment page to get by default.

Further down, we use our variable in the call to paginate_comments_link.

</pre>
<div></div>
<pre>
 

One more thing

Remember that WordPress keeps track of the number of comment-pages so that it sends new comments to the latest comment-page? We need to tell WordPress to re-calculate the comments-pages. Turns out we can filter get_comment_link.

function thematic_get_comment_link( $link , $comment, $args ) {
globalĀ  $wp_rewrite;

$args['type'] = 'comment';
$args['page'] = get_page_of_comment( $comment->comment_ID, $args );

if ( $wp_rewrite->using_permalinks() )
$link = user_trailingslashit( trailingslashit( get_permalink( $comment->comment_post_ID ) ) . 'comment-page-' . $args['page'], 'comment' );
else
$link = add_query_arg( 'cpage', $args['page'] , get_permalink( $comment->comment_post_ID ) );

return $link;
}
add_filter( 'get_comment_link', 'thematic_get_comment_link', 10, 3 );

The important bit here is

$args['type'] = 'comment';

This makes sure that the comment-paging calculations will be done on comments only (and not trackbacks) and send the visitor to the correct place. This will also send them to the correct comment-page after replying to someone else’s comment.

So there you have it! There is still room for some fine-tuning of details, but our testing indicates that it works like it should. Hopefully not too many people were affected by the previous behaviour, but for those who were this update will be very welcome.

2 Comments

  1. Posted June 30, 2012 at 12:45 am | Permalink

    There doesn’t seem to be a changelog available for Thematic 1.0.2 – could you post some details about that please?

    • middlesister
      Posted July 5, 2012 at 3:09 pm | Permalink

      The changelog can be found in the readme. There is not too much change from 1.0.1. New filters for header and footer open and close tag, addition of a couple of language files, pot file in sync again, and the moving of thematic_doctitle to be added as a filter to wp_title. This was to comply with theme review guidelines for wp 3.4.




Post a Comment

Your email is never published nor shared. Required fields are marked *

*
*

You may use these HTML tags and attributes <a href="" title=""> <blockquote cite=""> <cite> <code> <em> <strong> <pre class="" title=""> <p> <del> <ins> <ul> <ol> <li>