We use cookies to improve our services and provide a better experience. By continuing to use this site, you agree with our Cookies Policy.

Tutorials
More than 400 step-by-step articles to guide you through online project development.
HomeTutorialsCMS TutorialsWordPressWordPress Administration

How to create a comment history

KVM VPS

Here we will show how easily we can create a comment history in the WordPress website. Actually, this can be done by creating separate page dedicated only for comments history and using custom code for displaying all comments. The first one can be accomplished in less than five minutes and doesn’t require advanced programming knowledge.

The steps are as follows:

Follow these steps to create a comment history

Navigate to the current theme folder which usually is located in the public_html directory of WordPress installation. For example, if the current active theme is Twenty Twelve, full path to themes directory is “public_html/blog/wp-content/themes/twentytwelve/”.

WordPress Theme Twenty Twelve Directory

Inside “twentytwelve” directory we should find another directory “page-templates” in which all templates are stored. There we will find default templates that are already included by default.

WordPress Page Templates

The main reason why we need to find this directory as we are going to create separate file for creating a comment history. In this case, we need to create new file here. Give it a name like “comment_history.php” and add the following lines of code:

1
2
3
4
5
<?php
/*
Template Name: Comment History
*/
?>

Then go to WordPress dashboard and create new page by clicking Pages>Add New. Enter page title and select “Comment History” template in the “Page Attributes” box.

WordPress Page Attributes

We have selected exactly the same template which was created in the previous steps. This will be the page in which we will be displaying comments history.

Second part covers code entry for getting all comments and displaying them in the page. The code can be used as in the following example:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
<?php
 
if( is_user_logged_in() ) :
	global $current_user;
	get_currentuserinfo();
 
	$querystr = "
	SELECT comment_ID, comment_post_ID, post_title, comment_content
	FROM $wpdb->comments, $wpdb->posts
	WHERE user_id = $current_user->ID
	AND comment_post_id = ID
	AND comment_approved = 1
	ORDER BY comment_ID DESC
	 ";
 
	$comments_array = $wpdb->get_results($querystr, OBJECT);
 
	if( $comments_array ): ?>
<h2>Your Recent Posts</h2>
 
<?php
		$count = 0;
		$page = 0;
		foreach ( $comments_array as $comment ):
			if($count++ % 0) $page++;
			setup_postdata( $comment );
			echo "<li>comment_post_ID.($page > 1 ? '-page-'.$page : '')
."#comment-".$comment->comment_ID."'>Comment on ". $comment->post_title
. "<br />". $comment->comment_content . "</li>";
		endforeach; ?>
<?php
	endif;
endif;
?>

The mentioned code entry will display recent comments in our newly created page if we will implement this code in that page. This way all of our comments will be displayed in one page like comments history.

Linux Cloud Server
Rate this Tutorial:
No Comments Leave a Comment
Leave a Comment

Other (182)

Popular Keywords