1
votes

I want to have a custom block that shows the teaser of exactly one randomly-selected node from a certain taxonomy. I found http://drupal.org/node/135344 but it only displays a series of node titles. How do I display the teaser for a random one of the nodes instead?

I'm using Drupal 6 including i18n. I do not want to use the Views module because I plan to customize the look of this a lot. Thank you for helping a newbie!

1

1 Answers

1
votes

Using the link you gave, I came up with a quick module to display a random node for a given taxonomy term in a block. If you have your own module going, you may only need the part given in get_block_content(). The module is called *test_block* and these files are located within 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.');
}

For more options on the node display, see node_view()