So, this is complicated: There’s two parts of our site, one part is in WordPress (posts/pages), the other is reviews, cast pages, and search results (which I wrote the code myself for).
They share a centralized navigation system, where I want to include recent comments. I could use a Plugin, but that won’t work for the pages outside of WordPress.
So, as I did with the “Recent Posts” section, I’m trying to write my own code.
I’ve got it working, without issue, but want to replace the “comment_content”, with the permalink to the post. Since the permalink’s not kept in the comment data (but linked via the ID, I’m assuming), I don’t know how to do it.
Here’s the code I’ve got now, that lists the last 10, but excludes my two main authors:
// set your infomation.
$dbname = 'xxxxxxxxxxxxx';
// connect to the mysql database server.
mysql_connect ($dbhost, $dbusername, $dbuserpass);
//select the database
mysql_select_db($dbname) or die('Cannot select database');
$search = $_GET['search'];
$keywords = explode(" ", $search);
$query = "SELECT * FROM wordpress_comments WHERE comment_approved='1' AND user_id != '3' AND user_id != '2' ORDER BY comment_ID DESC LIMIT 10" ;
$result = mysql_query($query) or die(mysql_error());
while($row = mysql_fetch_array($result))
echo "<img src=\"../images/sub_bullet.gif\" alt=\"\"><a href=\"";
echo "</a> ";
I know it’s not the most elegant code in the world, but it (sort of) works for what I want to do. Can someone point me in the right direction on how to get the permalink from the info available in the comments data?
My preference for doing this is just to use the comments feed, and then use an open source package like Magpie RSS to display it on the page. That would have the added advantage of caching the recent comments rather than executing that query on every page load.
Not a direct answer to your question, but then, I don’t know the direct answer.
Edit: Looks like you would want the “comment_post_ID” field from the comments table, then look up the post URL from the posts table.