Community Documentation

Display (x) nodes from a multiple categories

Last updated October 26, 2009. Created by Dublin Drupaller on September 18, 2005.
Edited by esmerel, pwolanin. Log in to edit this page.

PLEASE NOTE! The following snippet is 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.

This php snippet displays nodes from a list of categories specified in the first line:

<?php
/**
* This php snippet displays nodes
* from a list of categories specified in the first line ($taxo_id)
*
* To increase/decrease the number of nodes listed
* change the $list_length value to suit.
*
* Works with drupal 4.6.x & 4.5.x
*
* Snippet submitted by Robert Garrigos (robertgarrigos)
* Updated by Sean Robertson (seanr)
*/
$taxo_id = "3,4,5"; /* comma seperated list */
$list_length = 1;
$sql = "SELECT * FROM {node} INNER JOIN {term_node} ON {node}.nid = {term_node}.nid WHERE {term_node}.tid in ($taxo_id) ORDER BY {node}.created DESC LIMIT $list_length";
$result = db_query($sql);
while (
$anode = db_fetch_object($result)) {
$output .= theme('node', $anode, $teaser = FALSE, $page = FALSE);
}
print
$output;
?>

Comments

this snippet picks nodes

this snippet picks nodes classified under either taxo_id = 3;
or taxo_id = 4; or taxo_id = 5;

i.e. = a broader selection (OR principle)

i am looking into a snippet picking nodes classified under both taxo_id = 3; and taxo_id = 4; and taxo_id = 5;

i.e. = a tighter selection (AND principle)

i'll keep searching (or i'll learn php myself....)

This does it

I ripped this right out of the taxonomy.module

<?php
$taxo_id
= "12,28"; /* comma seperated list */
$list_length = 3;

$joins = '';
$wheres = '';
$taxo_ar = explode(",", $taxo_id);
foreach (
$taxo_ar as $index => $tid) {
  
$joins .= ' INNER JOIN {term_node} tn'. $index .' ON n.nid = tn'. $index .'.nid';
  
$wheres .= ' AND tn'. $index .'.tid =' . $tid;
}
$sql = 'SELECT DISTINCT(n.nid), n.sticky, n.title, n.created FROM {node} n '. $joins .' WHERE n.status = 1 '. $wheres .' ORDER BY n.sticky DESC, n.created DESC LIMIT ' . $list_length;

$result = db_query($sql);
while (
$anode = db_fetch_object($result)) {
$output .= theme('node', $anode, $teaser = TRUE, $page = FALSE);
}
print
$output;
?>

No nested Selects...

The Same but with Teasers

The same as above, but with author, teasers and specific node type ($charlength defines the teaser longitude in chars)

<?php
$taxo_id
= "1,18"; /* comma seperated list */
$list_length = 10;
$charlength="150";
$content_type = "story";

$joins = '';
$wheres = "AND n.type = '$content_type' ";
$taxo_ar = explode(",", $taxo_id);
foreach (
$taxo_ar as $index => $tid) {
 
$joins .= ' INNER JOIN {term_node} tn'. $index .' ON n.nid = tn'. $index .'.nid';
 
$wheres .= ' AND tn'. $index .'.tid =' . $tid;
}
$sql = 'SELECT DISTINCT(n.nid), n.sticky, n.title, nr.teaser, n.created, u.name FROM {node} n JOIN {node_revisions} nr ON (n.nid = nr.nid and n.vid = nr.vid) INNER JOIN {users} u ON n.uid = u.uid '. $joins .' WHERE n.status = 1 '. $wheres .' ORDER BY n.sticky DESC, n.created DESC LIMIT ' . $list_length;

$result = db_query($sql);
while (
$anode = db_fetch_object($result)) {
$output .= "<p>" . l($anode->title, "node/$anode->nid") . "<br>" . substr(strip_tags($anode->teaser), 0, $charlength) ."...<em> Submited by: <strong>" . $anode->name . "</strong></em></p>";
}
print
$output;
?>

This should do the trick.

This should do the trick. The following code

  $myNodes = taxonomy_select_nodes(array(8,4),"and");
  print taxonomy_render_nodes($myNodes);

will render all nodes that belong to taxonomy 4 and 8.
If you want to theme the output specifically you may need to loop through the dataset returned ($myNodes).

Page status

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