【发布时间】:2014-05-01 19:04:59
【问题描述】:
前段时间,我为我的 WordPress 安装编写了这个 sn-p,以将 5 个最新的 cmets 显示为一个列表:
<?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>
今天安装了Disqus,cmets的链接改了。同样的评论,之前链接是http://mydomain.com/?p=760/#comment-4986,现在是http://mydomain.com/?p=760/#comment-1364246021。
由于链接不再有效,我编写了这个 sn-p 以通过 Disqus API 获取 5 个最新帖子。它工作正常。我得到作者和评论内容。
<?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++;
}
}
?>
我还想获得 cmets “属于”的 WordPress 帖子的链接! 例如,最后一条评论发布在 id=44 的博客文章上。我想拥有该 ID 或更好的完整永久链接(例如 domain.com/?p=44)。我搜索了他们的 API,但找不到可以检索帖子永久链接/id 的内容。
编辑:好的,看来我需要将posts/list 与threads/list 混合使用。 但我不知道如何从第一个 foreach(帖子/列表)中提取线程名称,将它们存储在一个数组中并将其放入另一个 foreach(这次是线程/列表)以获得(对于每个)响应->链接。似乎我需要有 5 个不同的请求(对于每个请求,随着线程的变化,我需要一个不同的端点)。
【问题讨论】:
标签: php wordpress comments disqus