How to link to last comment on a node page? I have some nodes with lots of comments that use a pager. I want to link from the top of the node page to the last comment. 

Is there a simpler way than building the link like this (source):


Get number of (published) comments for a certain node;
Get number of comments per page (it can differ from on content type to another);
1+2 = number of pages;
Get cid of the last comment;
Create a link like /node/[nid]?page=[page]#comment-[cid]
I don't think there's a simpler way than that, but you can combine two of those steps into one so you at least only need to make a single database call:

function node_last_comment_link($node, $link_text) {
  // Get the num active comments and most recent comment id
  $data = db_query('SELECT MAX(cid) AS cid, COUNT(cid) AS cnt FROM {comment} WHERE nid = :nid AND status = 1', array(':nid' =&gt; $node-&gt;nid))-&gt;fetchObject();

  // No comments, no link
  if ($data-&gt;cnt == 0) {
    return FALSE;
  }

  // Get the num comments per page
  $var_name = 'comment_default_per_page_' . $node-&gt;type;
  $sensible_default = 50;
  $num_per_page = variable_get($var_name, $sensible_default);
  $page_num = floor($data-&gt;cnt / $num_per_page);

  //Build and return a URL
  $query = array();

  // No point adding ?page=0
  if ($page_num &gt; 0) {
    $query['page'] = $page_num;
  }

  $fragment = 'comment-' . $data-&gt;cid;
  return l($link_text, 'node/' . $node-&gt;nid, array('query' =&gt; $query, 'fragment' =&gt; $fragment));
}


I've given that a quick test and it seems to work but it might need a bit of massaging, especially if you have threading turned on :)