Verizon cell phones

Generating Teasers Lists In Drupal

Drupal logo
Looking to generate teaser lists in Drupal? Here is some code to accomplish this. These are very useful if you want to create custom blocks of teaser lists anywhere on a page. Simply call the function and it will render the output.


Generate Teaser

This piece of Drupal code will grab one teaser.

function GetTeaser($contentType, $nodeId) {
$result = db_query_range(db_rewrite_sql("SELECT * FROM {node} n WHERE n.type = '{$contentType}' AND n.status = 1 AND n.nid={$nodeId}"), 0, 1);
$node = db_fetch_object($result);
echo node_view(node_load(array('nid' => $node->nid)), 1);
}

Generate Teaser Lists

This Drupal snippet will grab a range of teasers.


function GetLatestTeasers($contentType, $startEntry, $lastEntry) {
$result1 = db_query_range(db_rewrite_sql("SELECT n.nid, n.created FROM {node} n WHERE n.type = '{$contentType}' AND n.status = 1 ORDER BY n.created DESC"), $startEntry, $lastEntry);
while ($node = db_fetch_object($result1)) {
$output2 .= node_view(node_load(array('nid' => $node->nid)), 1);
}
echo $output2;
}

Generate Random Teaser Lists

This Drupal snippet will grab a random range of teasers.


function GetLatestTeasersRandom($contentType, $num) {
$result1 = db_query(db_rewrite_sql("SELECT n.nid, n.created FROM {node} n WHERE n.type = '{$contentType}' AND n.status = 1 ORDER BY RAND() LIMIT {$num}"));
while ($node = db_fetch_object($result1)) {
$output2 .= node_view(node_load(array('nid' => $node->nid)), 1);
}
echo $output2;
}

How To Use Drupal Teasers

This assumes that you have used CCK to create a cookies type:

Cookies

<?php echo GetLatestTeasersRandom("cookierecipes", 4)?>

This assumes that you have the blog module running:

Latest News

<?php echo GetLatestTeasers("blog", 0, 4); ?>

Filed under: