WP-Mix

A fresh mix of code snippets and tutorials

Display all comments on separate page

Normally WordPress themes display comments on the same post or page. WordPress makes it possible to page the comments, so that there are only so many per page, but by default it’s not possible to display all comments on a separate page. Here’s how to do it in three easy steps.

Step 1: Edit single.php

Open single.php and wrap the contents of the WordPress loop, like so:

<?php if (have_posts()) : while (have_posts()) : the_post(); ?>

	<?php if (!isset($_GET['show'])) { ?>
		<article id="post-<?php the_ID(); ?>" <?php post_class(); ?>>
			<h1><a href="<?php the_permalink(); ?>"><?php the_title(); ?></a></h1>
			<?php the_content(); ?>
			<p><a href="?show=comments">Click here to view/write comments</a></p>
		</article>
	<?php } ?>

<?php endwhile; ?>

	<?php if (isset($_GET['show']) && $_GET['show'] == "comments") { ?>
		<?php comments_template(); ?>
	<?php } ?>

<?php else : endif; ?>

Step 2: Edit comments.php

Next open comments.php and modify the comments form as follows:

<form action="<?php echo get_option('siteurl'); ?>/wp-comments-post.php" method="post" id="commentform">
		.
		.
		.
	<textarea name="comment" id="comment" placeholder="Your Comment Here..." tabindex="4"></textarea>
	<input name="submit" type="submit" id="submit" tabindex="5" value="Submit Comment">
	<?php comment_id_fields(); ?>
	<?php do_action('comment_form', $post->ID); ?>

	<?php if (function_exists('curPageURL')) { ?>
		<input type="hidden" name="redirect_to" value="<?php echo curPageURL() ?>" />
	<?php } ?>

</form>

Step 3: Edit functions.php

Lastly, open functions.php and add the following function:

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;
}

And that’s all there is to it. For more information, visit the original post.

Learn more

Digging Into WordPressWordPress Themes In DepthWizard’s SQL Recipes for WordPress