Take the 2-minute tour ×
WordPress Development Stack Exchange is a question and answer site for WordPress developers and administrators. It's 100% free, no registration required.

I would like to have a separate page for the comments on a post, such that this page: /2011/02/post-name/ just shows the post and then you click a link to view the comments, taking you to a page like this: /2011/02/post-name/comment-page-1/

I will then check the URL to see if "comment-page-x" is in it, and display the page differently if so (remove the post, so people don't have to scroll past that every time, and also style things a bit differently).

This would work, but WordPress redirects comment-page-1 to the post page - I need it to keep comment-page-1 there in the URL.

Is this possible? How can I do it? Thanks!

share|improve this question
1  
upvotet the Q. As Mike said: good one :) –  kaiser Feb 11 '11 at 17:07
add comment

1 Answer

up vote 7 down vote accepted

Hi @Shaun:

Great question! WordPress assigns your comment page number to the query var 'cpage' which gets set when your URL has /comment-page-1/ at the end. So your culprit is in the redirect_canonical() function, line 192 of /wp-includes/canoncial.php.

if ( get_query_var('paged') || is_feed() || get_query_var('cpage') ) {

Since the redirect_canonical() function gets set as an action what we can do is to insert our own function to be called instead, have our function set the 'cpage' query var to false, call redirect_canonical(), and then set 'cpage' back to what it was; that will keep WordPress from redirecting on you.

To insert your own function you need to call the two hook 'init' and 'template_redirect' like so being sure to set the 'init' hook to be called after the do_action() inside WordPress core that adds redirect_canonical():

add_action('init','yoursite_init',11); //11=lower priority
function yoursite_init() {
  remove_action('template_redirect','redirect_canonical');
  add_action('template_redirect','yoursite_redirect_canonical');
}

add_action('template_redirect','yoursite_redirect_canonical');
function yoursite_redirect_canonical($requested_url=null, $do_redirect=true) {
  $cpage = get_query_var('cpage');
  set_query_var('cpage',false);
  redirect_canonical($requested_url, $do_redirect);
  set_query_var('cpage',$cpage);
}

Then of course you need to do something with your 'cpage'. You can either check for the value returned by get_query_var('cpage') or you can add another hook to allow you to create a comment-specific template which is what I did. It will add look for a theme template file with the same as it would normally load but with [comments].php at the end of the name instead of .php, i.e. single[comments].php. Note that I set priority for this filter to be 11; you may need to set to an even larger number if a plugin you use adds itself after your hook:

add_filter('single_template','yoursite_single_template',11);
function yoursite_single_template($template) {
  if (get_query_var('cpage'))
    $template = str_replace('.php','[comments].php',$template);
  return $template;
}

And here's the proof that it all works!

Screenshot of Custom WordPress Page Template for Comments

share|improve this answer
    
Short.... wow! Too bad we can't do a +10 over here... –  kaiser Feb 11 '11 at 14:35
    
Thanks for the detailed response, but a question - where can I put this code? I've tried in functions.php but not having much luck. Assuming I'm using twentyten theme, where in the file do I add the code? –  Shaun Feb 11 '11 at 14:41
    
@Shaun - Figures. :) The one time I didn't add the following text to my answer I get called for it: "You can add the following code to your theme's functions.php file or in a .php file of a plugin you might be writing." But you say that is not working? What exactly is it not doing? Have you put echo "Test"; exit; statements in each of the functions to make sure the hooks are firing? Do you have plugins that may be conflicting? Did you try changing 11 in two places to 100? Did you include all the code? Did you create a theme template file called single[comments].php? –  MikeSchinkel Feb 11 '11 at 15:08
    
@kaiser - Thanks. :) –  MikeSchinkel Feb 11 '11 at 15:10
    
@MikeSchinkel Brilliant, thanks. I did not create the single[comments].php! –  Shaun Feb 11 '11 at 17:14
show 1 more comment

protected by Community Jun 27 '11 at 8:16

Thank you for your interest in this question. Because it has attracted low-quality answers, posting an answer now requires 10 reputation on this site.

Would you like to answer one of these unanswered questions instead?

Not the answer you're looking for? Browse other questions tagged or ask your own question.