【发布时间】:2012-03-12 19:13:19
【问题描述】:
我想要一个自定义块来显示从某个分类中随机选择的一个节点的预告片。我找到了http://drupal.org/node/135344,但它只显示一系列节点标题。如何改为显示随机节点之一的预告片?
我正在使用 Drupal 6,包括 i18n。我不想使用 Views 模块,因为我计划自定义它的外观。感谢您帮助新手!
【问题讨论】:
我想要一个自定义块来显示从某个分类中随机选择的一个节点的预告片。我找到了http://drupal.org/node/135344,但它只显示一系列节点标题。如何改为显示随机节点之一的预告片?
我正在使用 Drupal 6,包括 i18n。我不想使用 Views 模块,因为我计划自定义它的外观。感谢您帮助新手!
【问题讨论】:
使用您提供的链接,我想出了一个快速模块,用于在块中显示给定分类术语的随机节点。如果您有自己的模块,您可能只需要get_block_content() 中给出的部分。该模块名为 *test_block*,这些文件位于 sites/all/modules/test_block 中
test_block.info
name = Test Block
description = Display a random block for given taxonomy term(s)
dependencies[] = node
core = 6.x
test_block.module
<?php
/**
* Implementation of hook_block()
*/
function test_block_block($op='list', $delta=0, $edit=array()) {
switch ($op) {
case 'list':
$blocks[0]['info'] = t("Random Node Block");
return $blocks;
case 'view':
$blocks['subject'] = t("Random Node Block");
$blocks['content'] = get_block_content();
return $blocks;
}
}
/**
* Return the HTML teaser of a random node
* for a given taxonomy term
*/
function get_block_content() {
// Comma separated lists of terms tid to display nodes
$terms = '4,6';
$sql = "SELECT n.nid FROM {node} n INNER JOIN {term_node} tn ON n.nid = tn.nid WHERE tn.tid IN ($terms) AND n.status = 1 ORDER BY RAND() LIMIT 1";
$nid = db_result(db_query($sql));
if ($nid) {
// Return the node teaser
return node_view(node_load($nid), TRUE);
}
return t('No nodes available for the term(s) given.');
}
更多关于节点显示的选项,见node_view()
【讨论】: