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