Community Documentation

Display a list of (x) node titles of a specific type

Last updated April 11, 2007. Created by punboy@aaron.pu... on June 9, 2005.
Edited by add1sun, pwolanin, Dublin Drupaller. Log in to edit this page.

PLEASE NOTE! The following snippets are user submitted. Use at your own risk! For users who have setup drupal using an alternate database to the default (MYSQL), please note that the snippets may contain some database queries specific to MYSQL.

<?php
/**
* Creates a list of node titles of a specific type
* with a link to each node.
*
* To change which type is listed, simply edit the $node_type string.
* To change the number of node titles listed, simply edit the $list_no number.
*
* This works with Drupal 4.5 and Drupal 4.6
*/
$node_type = "flexinode-1";
$list_no =5;
$sql = "SELECT node.title, node.type, node.nid FROM {node} WHERE node.type = '$node_type' LIMIT $list_no";
$output .= "<ul>";
$result = db_query($sql);
while (
$anode = db_fetch_object($result)) {
$output .= "<li>".l($anode->title, "node/$anode->nid")."</li>";
}
$output .= "</ul>";
print
$output;
?>

To list them in descending order

<?php
/* list of recent posts of node type 'flexinode-1'
** output in descending chronological order
*/
unset ($output); // clear previous output
$node_type = "flexinode-1";
$list_no =5;
$sql = "SELECT node.title, node.type, node.nid FROM {node} WHERE node.type = '$node_type' AND node.status = 1 ORDER BY node.created DESC LIMIT $list_no";
$output .= "<ul>";
$result = db_query($sql);
while (
$anode = db_fetch_object($result)) {
$output .= "<li>".l($anode->title, "node/$anode->nid")."</li>";
}
$output .= "</ul>";
print
$output;
?>

Comments

Version for Drupal 7 ?

Version for Drupal 7 ?

drupal still uses mysql, so

drupal still uses mysql, so any version will work rigth if you are using plain php and mysql

About this page

Drupal version
Drupal 4.5.x or older, Drupal 4.6.x

Archive

Drupal’s online documentation is © 2000-2012 by the individual contributors and can be used in accordance with the Creative Commons License, Attribution-ShareAlike 2.0. PHP code is distributed under the GNU General Public License. Comments on documentation pages are used to improve content and then deleted.
nobody click here