Warning: Call-time pass-by-reference has been deprecated; If you would like to pass it by reference, modify the declaration of module_invoke_all(). If you would like to enable call-time pass-by-reference, you can set allow_call_time_pass_reference to true in your INI file in /home/.megatoes/geniusy/geniusy/drupal/modules/fuzzysearch/fuzzysearch.module on line 324
Geniusy's Blog | 不是站长用Drupal,而是Drupal折磨站长!

Drupal如何调用文章阅读次数和评论次数

初次接触Drupal的新手可能会和我一样手足无措,本来很简单的功能都没办法实现。调用阅读数和评论数真是很简单的功能,如果使用其它CMS或者Blog CMS,这些一般都能从官方默认的模板或者使用帮助中找到,但是Drupal就不同了,什么都得靠自己摸索。

为了调用这两个变量我首先想到的是在数据库里面找,但是非常令我失望,drupal的node表里面根本没记录阅读次数,评论到是可以通过count一下comments表里面每个nid出现的次数得到,但是每次那样也未必烦琐了一点。

在DrupalChina里面提出了这个问题,第一个问题得到了解答:先要在后台,“管理”-“日志”-“访问记录设置”里面启用“页面访问计数”功能,然后就可以通过<?php
print $node->links['statistics_counter']['title']
?>语句调用node阅读次数了。

显示评论数是我自己摸索出来的,在api.drupal.org上面找到了三个函数,comment_num_all,comment_num_new和comment_num_replies,我只用了第一个,后面两个具体什么功能没用过,官方上也没说明。但我要的功能达到了,在需要显示评论数的地方加上<?php print comment_num_all($node->nid) ?>语句就OK了。

上次在api.drupal.org上还找到了一个显示分类的函数,我现在右边的分类就是用taxonomy_get_tree($vid)和taxonomy_get_children($vid)来获取分类(category或者taxonemy),这样就可以不需要自己写SQL语句了,其实这些函数就是由一些SQL语句组成的。有空的时候可以多到api网站上转转,上面可以找到很多好用的函数的。

Tags: api, comment_num_all, drupal, node, statistics_counter, taxonomy_get_tree, 文章, 次数, 评论数, 调用, 阅读

回复

此内容将保密,不会被其他人看见。
  • You can use BBCode tags in the text, URLs will automatically be converted to links.
  • You can use coolfilter tags in the text, to include code and media
  • Image links from G2 are formatted for use with Lightbox.V2
  • Allowed HTML tags: <a> <em> <strong> <cite> <code> <ol> <dl> <dt> <dd> <img> <inline> <center> <p> <object> <br> <th> <tr> <td> <embed>
  • 网页地址和电子邮件地址将会被自动转换为链接。

更多格式化选项信息