I'm using the following code to generate a listing pages of recent blog entries.

$listlength="10";
$content_type = 'blog';
unset ($output);

$prev_day = '';
$result1 = pager_query("SELECT n.title, n.nid, n.uid, n.created, u.name, c.comment_count 
                                        FROM {node} n INNER JOIN {users} u ON n.uid =u.uid 
                                        INNER JOIN {node_comment_statistics} c ON n.nid = c.nid 
                                        WHERE n.type = '$content_type' AND n.status = 1 
                                         ORDER BY n.created DESC");

while ($node = db_fetch_object($result1)) {

$day = format_date($node->created, 'custom', 'l, jS F');

if ($day != $prev_day) {
    $output .= "<h2>$day</h2>";
    $prev_day = $day;
  }

$output .= "<p>" . l($node->title, "node/$node->nid") . "<br>" . "<small>» Posted by " . l($node->name, "user/$node->uid") .  ". There " . format_plural($node->comment_count, 'is 1 answer', 'are %count answers') . " so far." . "</small></p>";

}
$output .= theme('pager', NULL, $listlength);
print $output;

The output is:

Day, xth of the Month
Title of node (url with title)
» Posted by USER (link to user). There are (comment count) answers so far.

I would like to also include the taxonomy terms in which the blog post has been made:

Day, xth of the Month
Title of node (url with title)
» Posted by USER (link to user) in Taxonomy Term1, Taxonomy Term 2. There are (comment count) answers so far.

Now, I see in the API that there is a function called taxonomy_node_get_terms which performs this function, however, I am uncertain how to include it in the query.

Any help much appreciated.

Comments

C-Watootsi’s picture

Anyone? Man, these SQL questions are pretty unpopular.

C-Watootsi’s picture

*bump*

nevets’s picture

Using 4.6 you can

$term_set = taxonomy_node_get_terms(27);

$term_list = "";
foreach ( $term_set as $term ) {
  if ( $term_list ) {
    $term_list .= ', ';
  }
  $term_list .= l($term->name, "taxonomy/term/$term->tid");
}
if ( $term_list ) {
  $term_list = "In " . $term_list;
}

Place this before the line that sets $output and add $term_list where you want them.

(side note, in 4.7 the terms are already in $node->taxonomy)

C-Watootsi’s picture

Doesn't quite work yet, but that's probably me being stupid. Thanks for taking the time Nevets.

--Case.

nevets’s picture

Minor point, replace the 27 (first function call) with the node id you want terms for. Possibly use $node->nid depending on the context.