0

我想为启用了评论的节点上显示的“添加评论”和“评论”链接设置主题。我知道有theme()theme_links()可以帮助解决这个问题,但我不知道如何使用它们。我很确定我想要theme_links(),因为在这种情况下我正在寻找链接。但是如何具体获取评论链接?我不想为所有链接设置主题,只是评论中的链接。如果有帮助,我的目标是在每个链接旁边添加一张图片。另外,在“评论”旁边,我想包括发布的评论数量。

为了澄清,我想为出现在Node上的链接设置主题,而不是出现在 Comments 本身上的链接。

4

3 回答 3

3

To add an image/icon to a link, you can use simple CSS. This CSS will add an icon to the "Add Comment" link, but the same could be done for other links as well (li.comment_delete, li.comment_edit, etc).

ul.links > li.comment_add > a {
  background: url(PATH TO IMAGE) no-repeat;
  padding-left: 20px;  /* Change to compensate for size of image */
}

To add the number of comments on a node you can use the function comment_num_all($node->nid). For instance if you would like to add the number of comments to the "Add comment" link, you could add a hidden DIV to the node.tpl.php (or each content type template) and jQuery to edit the link text:

<div id="num-comments" style="display:none;"><?php print comment_num_all($node->nid); ?></div>

jQuery:

$('ul.links > li.comment_add > a').text('Add new comment (' + $('#num-comments').text() + ')');

This is not the most elegant solution, but it works. If you want to use theme_links() I think you would have to create a custom module.

EDIT: Another option is to create a custom module. This does not use theme_links(), but hook_link_alter() instead. This is a small example module to change the title of the "Add new comment" link, add an icon and include the number of current comments attached to the node: (Replace every instance of MYMODULE_NAME with the name you choose for the module)

STEP 1: Create a file called MYMODULE_NAME.info and add:

name = "MYMODULE_NAME"
description = "Change the appearance of links that appear on nodes"
core = 6.x

STEP 2: Create file called MYMODULE_NAME.module and add:

<?php

  /**
   * Implementation of hook_link_alter
   */
  function MYMODULE_NAME_link_alter(&$links, $node){
    if (!empty($links['comment_add'])) {
      // Get number of comments for node
      $num_comments = db_result(db_query('
        SELECT comment_count 
        FROM {node_comment_statistics} 
        WHERE nid = %d
      ', $node->nid));

      // Set "Add new comment" link text
      $links['comment_add']['title'] = '<img src="PATH TO ICON"/> ADD COMMENT TEXT (' . $num_comments . ')';

      // Allow HTML in the link text
      $links['comment_add']['html'] = TRUE;
    }
  }

STEP 3: Put these files in a folder called MYMODULE_NAME, place the folder in sites/all/modules, and enable the module

EDIT: To find array keys: in your node.tpl.php (or any other node template) you can add <?php print_r($node->links); ?>. This will show you all of the link info to be displayed in the node, and the keys of the main array are what you would use in my module. You can also try using Firebug/Chrome Dev Tools, etc to look at the class of the list item containing the link (ie ul.links > li.comment_add). I believe when the links are constructed, Drupal uses the array key as a class for the link.

于 2011-02-27T23:50:09.500 回答
0

我认为最简单的做法是覆盖主题中的 comment.tpl.php 文件。您可以复制 /themes/garland 中的一个以用作基础。

于 2011-02-24T00:43:50.407 回答
0

我已经使用http://drupal.org/node/352020中的这种技术来创建相同类型的链接,你想在你的模块中添加一个预处理钩子来访问 $links 数组:

功能 yourmodule_preprocess_comment (&$variables) {
  $comment = $variables['comment'];

  //加载当前评论的链接
  $links = comment_links($comment, FALSE);

  //更改链接数组的代码

  //重置链接HTML
  $variables['links'] = theme('links', $links);
}
于 2011-02-25T04:15:05.290 回答