08 Nov, 2008
Put comments on a separate page (in wordpress)
Posted by: Jennifer In: WordPress|WordPress Hacks|WordPress Plugins|WordPress scripts
I've had a few clients ask for this, and I saw some requests to do this on the forums, but no one ever that I found came up with a solution. Here's the scenario, you have your blog posts, and maybe they're long, and rather than make the page *even longer* by dumping the comments on the same page at the bottom, you want a special, separate page for the comments for each post.
Now the trick I've done to accomplish this, I will warn you worked in the scenario it was designed in – (I had "permalinks" turned on. I'm not sure how this would work, if at all, if they weren't turned on, or if the permalink configuration was different) I'm sure there are probably other setups where this would not work. So, that's my disclaimer.
Basically what we're going to do is use the query string to determine whether or not to display comments or the post on the page. It's not terribly "pretty" – and maybe a htaccess expert can tell us how to accomplish the same task by adding a line or two in there to keep the URLs "pretty" – but in the meantime, I'm just happy to have it work at all!
So first – surround anything you would want only on the POST version of the page with this:
*UPDATED TO ADD: This problem/question has come up a number of times. These changes will probably go on your single.php template file (because thats the page that normally dispalys the comments.) If for some reason you don't have a single.php file, then your "single-post view" is probably being derived from the default single.php template file (in the default theme directory) – so it's probably safe to grab a copy of that one and make your changes to it, and then upload it to your theme's directory)
<?php if (!isset($_GET['show'])) { ?>
POST PAGE ONLY STUFF GOES HERE...
<?php } ?>
*ALSO UPDATED TO ADD:The code above should be WITHIN the "WP LOOP".
Inside there you will want to create a link that will bring the user to the "comment page" for the post. You do that like this:
<a href="?show=comments">Click here to view/write comments</a>
Then you wrap your call to the comments template like so:
<?php if (isset($_GET['show']) && $_GET['show'] == "comments") { ?>
<?php comments_template(); ?>
<?php } ?>
*UPDATED TO ADD: Take a look at where the comments_template() line normally goes on the default theme, if you're using a custom theme. You want to make sure you really only put that around the comments_template() line… not large sections of your single.php template file…
Now, ON your comments template page (comments.php), you will need to modify the comments form so that after a comment gets submitted, you end up back on the comment version of the page. To do that you need to grab the current URL of the page you're looking at – and put it into a hidden field (named "redirect_to") in the comment form. To get PHP to pull in the full URL, I grabbed this function from webcheatsheet.com:
function curPageURL() {
$pageURL = 'http';
if ($_SERVER["HTTPS"] == "on") {$pageURL .= "s";}
$pageURL .= "://";
if ($_SERVER["SERVER_PORT"] != "80") {
$pageURL .= $_SERVER["SERVER_NAME"].":".$_SERVER["SERVER_PORT"].$_SERVER["REQUEST_URI"];
} else {
$pageURL .= $_SERVER["SERVER_NAME"].$_SERVER["REQUEST_URI"];
}
return $pageURL;
}
To make that available throughout, I created a "plugin" file, and just threw that function into it. (obviously have to activate the plugin before you can start using that function in your theme). (There's probably another way to "add a function" like that, but this worked for me) If you don't already have one, create a "functions.php" file for your theme and add that function in there (surrounded by <?php and ?> tags obviously)
So then before the </form> of the comment form I added this:
<?php if (function_exists('curPageURL')) { ?>
<input type="hidden" name="redirect_to" value="<?php echo curPageURL() ?>" />
<?php } ?>
That's it. Hope that helps someone else.
Updated:
Here are the modifications applied to the default wordpress theme. I've made comments to the changes I added in so you can see what I did and then apply it as appropriate to your own theme. In the zip are JUST the files I modified – not the whole wordpress default theme – so single.php, index.php, functions.php and comments.php.