1

我需要建立并打印任何给定评论的深层链接。这样用户只需单击链接即可直接访问特定评论。我找不到原生的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"));

查询参数将添加一个?到 url。所以你的路径看起来像

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

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

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

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

4

2 回答 2

0

这基本上就是这样做的方法。评论固定链接的形式为:

node/<nid>#comment-<cid>

where<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
);
于 2010-09-17T22:29:57.397 回答
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 文件中。

于 2013-03-02T23:34:37.093 回答