Tutorials

Recent comments

The following tutorial will show you how to add a small list of the 5 most recent comments to the sidebar of your Movable Type Blog (MySQL Version only). There are already many scripts doing this out there, plus it’s a native feature of MT, you can do this without using PHP and MySQL as well.

However, none of the scripts I found enabled you to exclude yourself from the list. Personally, I reply to every comment on my blog, but I hate to see all my own comments in the sidebar. So, what this script does is get the 5 (although you can change this number easily) most recent comments made by anyone but you. :)

You need to be using Movable Type with the MySQL Database for this, and your pages need to have a .php extension.
Open up the file that contains your sidebar for your blog, and add the following code to it:

<h2>Most Recent Comments</h2>
<?php
// your MT Blog ID. You can see what this is by logging into MT and going to the New Entry Screen for your blog. Look at the address bar, there should be a blog_id there
$blogID = 1; 

//the email address you fill in when you're posting comments. Any comments made with this address will not be displayed.
$your_email = "you@domain.com";

//the URL to your Archive pages. If your Individual Archive pages are located at http://domain.com/blog/archives/00001.php, the URL would be http://domain.com/blog/archives/
$archive_url = "http://domain.com/blog/archives/";

// the amount of recent comments you want to show. Set to 5 by default.
$many = 5; 

// name of your MT database
$database = "domain_mt";

// the username and password that have access to the above database
$mtuser = "domain_you";
$mtpassword = "PASSWORD";

// MySQL host
$mthost = "localhost";

// DON'T EDIT BELOW THIS LINE

$db = mysql_connect ($mthost, $mtuser, $mtpassword) or die ('I cannot connect to the database.');
mysql_select_db($database,$db); 

$most_recent = "SELECT * FROM mt_comment WHERE comment_blog_id=$blogID ORDER BY comment_created_on DESC";
$recentresult = mysql_query($most_recent);
$recentnum = mysql_num_rows($recentresult);

$i=0;
while ($i < $many) {
$recent_author = mysql_result($recentresult,$i,"comment_author");
$recent_email = mysql_result($recentresult,$i,"comment_email");
$recent_url = mysql_result($recentresult,$i,"comment_url");
$recent_text = mysql_result($recentresult,$i,"comment_text");
$recent_entry = mysql_result($recentresult,$i,"comment_entry_id");
$recent_comment = mysql_result($recentresult,$i,"comment_entry_id");

if ($recent_email != $your_email){
echo "<p><strong>::</strong> ";
 if ($url != " "){
 echo "<a href=\"" . $recent_url . "\" target=\"_blank\">" . $recent_author . "</a>";
 } else { echo "<strong>" . $recent_author . "</strong>";}
echo " says: &quot;";
 $recent_text = substr($recent_text,0,70);
echo $recent_text . "...&quot; ";
$recent_entry = sprintf("%06d", $recent_entry);
echo "[ <a href=\"" . $archive_url . $recent_entry . ".php\">go</a> ]</p>";
 } else { $many = $many + 1;}

$i++; } 

?>

Just change the variables I commented on in the script to your own, and it should work like a charm. :)

Comments

Error Comments are closed for this entry.