Some time ago I wrote this snippet for my WordPress install to get the 5 latest comments to show up as a list:
<?php $comments = get_comments('status=approve&number=5'); ?>
<ul style="font-size:5px">
<?php foreach ($comments as $comment) { ?>
<li style="font-size:10px">
<div style="float:left;margin-right:3px"><?php echo get_avatar( $comment, $size = '35' ); ?></div>
<em style="font-size:12px"><?php echo strip_tags($comment->comment_author); ?></em> (<a href="<?php echo get_option('home'); ?>/?p=<?php echo($comment->comment_post_ID); ?>/#comment-<?php echo($comment->comment_ID); ?>">link</a>)<br>
<?php echo wp_html_excerpt( $comment->comment_content, 35 ); ?>...
</li>
<?php } ?>
</ul>
Today I installed Disqus, and the links to comments is changed. Same comment, before the link was http://mydomain.com/?p=760/#comment-4986
, now it’s http://mydomain.com/?p=760/#comment-1364246021
.
Since links were not working anymore I wrote this snippet to get the 5 latest posts through Disqus APIs. It works fine. I get author and comment content.
<?php
ini_set('display_errors', 'on');
$key="MY_PUBLIC_KEY";
$forum="MY_FORUM_NAME";
$limit = '5';
$endpoint = 'http://disqus.com/api/3.0/posts/list.json?api_key='.urlencode($key).'&forum='.$forum.'&limit='.$limit;
$j=0;
listposts($endpoint,$j);
function listposts($endpoint,$j) {
// Standard CURL
$session = curl_init($endpoint);
curl_setopt($session, CURLOPT_RETURNTRANSFER, 1);
$data = curl_exec($session);
curl_close($session);
// Decode JSON data
$results = json_decode($data);
if ($results === NULL) die('Error parsing json');
// Comment response
// print($data);
// Comment response
$comments = $results->response;
// Cursor for pagination
$cursor = '&cursor=' . $results->cursor->next;
$i=0;
foreach ($comments as $comment) {
$name = $comment->author->name;
$comment = $comment->message;
// Get more data...
echo "<em>".$name."</em><br/>";
echo $comment."<br/>";
$i++;
}
}
?>
I’d like to get also the link of the WordPress post the comments “belongs” to!
For example the last comment was posted on a blog post with id=44. I’d like to have that ID or better the complete permalink (e.g. domain.com/?p=44). I searched through their APIs but I can’t find something to retrieve post permalink/id.
EDIT: Ok, seems I need to mix posts/list with threads/list.
But I don’t know how to extract from the first foreach (posts/list) the threads names, store them in an array and and put it in the other foreach (this time for threads/list) to obtain (for each) response->link. Seems like I need to have 5 different requests (for every one I need a different endpoint as the thread changes).
In the
posts/list
endpoint, you can expand thethread
object which should give you the information you need. Just addrelated=thread
to your request.In the response,
thread
should now be an object instead of a string ID. Because you’re using WordPress, there should be anidentifiers
array in thethread
object that looks similar to this:"identifiers": [ "44 http://domain.com/?p=44" ]