在drupal块中,您可以使用node_load(arg(1))访问节点变量。但是如何在一个区块中获得评论变量呢?
答案 0 :(得分:0)
如果您需要获取节点的所有注释列表,可以使用以下代码:
$query = 'SELECT c.nid, c.subject, c.cid, c.timestamp FROM {comments} c INNER JOIN {node} n ON n.nid = c.nid WHERE c.nid = %d AND n.status = 1 AND c.status = %d ORDER BY c.cid DESC';
$result = db_query(arg(1), COMMENT_PUBLISHED);
while ($comment = db_fetch_object($result)) {
$comments[] = $comment;
}
而不是仅使用arg(1)
,而不考虑/node/<nid>/revision/<rid>
等网址中的节点修订,您应该使用menu_get_object()
;在这种情况下,代码将成为:
$node = menu_get_object();
if (!empty($node) && !empty($node->nid)) {
$query = 'SELECT c.nid, c.subject, c.cid, c.timestamp FROM {comments} c INNER JOIN {node} n ON n.nid = c.nid WHERE c.nid = %d AND n.status = 1 AND c.status = %d ORDER BY c.cid DESC';
$result = db_query($query, $node->nid, COMMENT_PUBLISHED);
while ($comment = db_fetch_object($result)) {
$comments[] = $comment;
}
}