Drupal:如何获得评论的深层链接?

时间:2010-09-17 17:04:06

标签: optimization drupal-6 drupal-theming

我需要构建并打印任何给定评论的深层链接。这样用户只需单击链接即可直接访问特定注释。我找不到原生的drupal函数来获得这个,所以我自己构建它。

我的解决方案

<?php
    global $base_url;
    $base = drupal_lookup_path('alias',"node/".$node->nid);
    $path = $base_url.'/'.$base.'#comment-'.$comment->cid;

    $link_options = array('html'=> $html);
    $commentlink = l($date, $path, $link_options);
?>

要打印链接,您只需拨打<?php print $commentlink;?>即可。但是我很确定有更好的和更多的drupal像解决问题的方法。

更好的方式

Mikeker做到了:)正如他在这里建议的那样是解决方案。

<?php
 $commentlink = l(
    $date,
    "node/$comment->nid",
    array("fragment" => "comment-$comment->cid"));
?>

请注意Mikeker和我的版本之间的微小差异。 array("fragment" => "comment-$comment->cid"));array("query" => "comment-$comment->cid"));

查询参数会在网址中添加?。所以你的路径看起来像

//…query
http://example.com/path/to/node?comment-2

与我的解决方案(片段)相反:

//…fragment
http://example.com/path/to/node#comment-2

注意: 不要在片段标识符中包含前导'#'字符。它将由drupal添加。

2 个答案:

答案 0 :(得分:0)

基本上就是这样做的方式。评论永久链接的形式为:

node/<nid>#comment-<cid>

其中<nid><cid>分别是节点和注释ID。您可以通过不致电drupal_lookup_path() - l()url()来为您自己做一步。缩短的例程如下:

$commentlink = l(
  $date,                                      // Text of the link
  "node/$node->nid",                          // path to node, l() handles aliases
  array('query' => "comment/$comment->cid"),  // fragment to specific comment
);

答案 1 :(得分:0)

如果有人想知道,Drupal 7方式(至少看起来像是这样):

<a href='http://YOURSITE.com/comment/CID#comment-CID'>link text</a>

例如:

print "<a href='/comment/$comment->cid#comment-$comment->cid'>text here</a>";

这可能会放在comment.tpl.php文件中。