Ya, so when I began making the comments.php
code I didn’t expect I’d have as much trouble as I did. I figured it would be super straight forward. It turned out I was surprised by how WordPress uses the Settings->Discussion options. I posted a wordpress.stackexchange.com question. So here’s the full story:
I created the file
comments.php
. Inside it I have a manual loop for my comments section. I loop fromoffset = 0 + ((page - 1) * total_per
tooffset + total_per
. The problem is when I add new comments, the comment form’s action field has the wrong URL generate. It consequently goes to the very last page of comments (ie. the oldest).I had a question regarding this that I posted earlier. However after tracing some of the WP source code around I was able to find more information, so I deleted the original.
How can I ensure my theme’s comment form generates the correct action URL without editing
wp-includes/comment-template.php
orwp-comments-post.php
?Line 734 in
wp-includes/comment-template.php
is causing the problem. I don’t understand why though. I changed my theme to copy cpage, so I am no longer messing with any expected states. What I do know is that I can resolve my problem by hacking up the line in the core file. This change makes the generated URL go to the correct location.
if ( 'newest' === get_option( 'default_comments_page' ) )
Originally this.
if ( 'oldest' === get_option( 'default_comments_page' ) && 1 === $cpage )
It doesn’t make sense to me that it disregards the cpage value for the URL, ONLY if the option is set to ‘oldest’. I would think it should consider when to disregard cpage in either situation. It doesn’t though, why is it hard coded to only consider when it is Oldest at the top and on page 1? This makes no sense!
What am I doing to cause
get_comment_link()
to generate the wrong page value? Is there a way for me to easily debug these things? I tried adding echos to the core code, but they didn’t show up.Here is all my comment form related source code. (I forgot to add the namespace for my functions “wbs”, use the power of imagination because I can’t edit the pastebin of it. comments.php(top) andfunctions.php(bottom) at http://pastebin.com/aer07uvF
Sorry I can only post 2 links with <10 rep.
edit:
So
comment_form()
generates the correct URL, because the comment-page-1 should always have the oldest comments.
I ended up resolving the whole debacle with very little outside help, so I ended up posting the answer too. Here’s the answer..
By default WordPress organizes the comment pages from oldest to newest. This does not change, even if the
Settings-Discussion
options have been modified. This is the sticking point, one might suspect changing these settings to reorganize the comment pages, it doesn’t. These settings, basically, define comment order in the default comment loop, and what page is displayed when$cpage
is empty.To be clear, when
$cpage
is empty that means you’re on the Post URL and not a comment page within that post.This means, the problem isn’t in getting the links, since that code is not dynamic. The problem is in how comments are displayed. There are two options
'default_comments_page'
and'comment_order'
each has two settings, which makes for four configurations. Each needs a unique offset calculation. In this code below you can see the offset calculations necessary to display the correct comments on the correct pages.
The code: