Print Taxonomy Terms in Blocks and page.tpl.php in Drupal 7

I recently have started working on a Drupal 7 project creating a comparatively simple brochure website. To my surprises a lot of familiar functions, syntax and platform for simple things have changed dramatically! Certainly many functions and handlers are become easier and simpler to do, however there are a lot of simple procedures gets to be more complicated.

Getting the Taxonomy term in page.tpl.php is one of them.

Drupal 6 - Taxonomy Term using $term object

Back in Drupal 6, Taxonomy terms are embedded in $node object. To print the taxnomy term, all you need to do is get it directly from the $node object:

<?php
foreach ( (array)$node->taxonomy as $term ) {
// $term->name has the taxonomy term name
// $term->tid has the taxonomy term id
}
?>

Drupal 7 - Taxonomy Term Reference CCK field

In Drupal 7 however, taxonomy terms are no longer embedded in $node object. They are now CCK fields that can be added to individual content type on the fly. However, it uses a CCK field type called "Term Reference", which means instead of storing the actually Taxonomy term in the Node, it stores the reference id of the taxonomy term. The actual Taxonomy terms are now now stored in a different table called taxonomy_term_data.

You can use the following snippet to retrieve the taxonomy term(s) in Drupal 7 instead.

<?php
// Load full $node object. IMPORTANT!
$node node_load($node->nid); 
// Get the reference id of the taxonomy term.
$tid $node->field_tags[$node->language][0]['tid']; 

// Query database table taxonomy_term_data with $tid from $node object.
$term db_query('SELECT n.name FROM {taxonomy_term_data} n WHERE n.tid = :tid', array(':tid' => $tid));

// db_query in Drupal 7 returns a stdClass object. Value names are corresponding to the fields in your SQL query (in our case "n.name")
foreach ($term as $record) {
  print 
$record->name;
}
?>

回應

Taxonomy links

Thank you for sharing
I have also found this code to display terms


<?php
            $node 
menu_get_object();
            if(
$node != null )
            {
            
$terms field_view_field('node'$node'field_tags');
            
$term_name =   $terms['#items'][0]['taxonomy_term']->name;
            print 
$term_name;
            }
            
?>

The problem is that how can I display terms as links ?

Thanks

Thanks for the code

Thanks for the code @guillaume! I didn't know Drupal 7 has handy Field API until recently. Drupal is getting sexier day by day ;)

Unfortunately I haven't run into the need of getting the links from terms so I dunno yet. Can anyone else help to enlight us? :)

Display Links

Hi guys.. I do something over here that fits on my needs. For display terms as links:


<?php
// Query database table taxonomy_term_data and taxonomy_index
// So I can get all terms from my node.
$term db_query('SELECT t.name, t.tid FROM {taxonomy_index} n LEFT JOIN  {taxonomy_term_data} t ON (n.tid = t.tid) WHERE n.nid = :nid', array(':nid' => $node->nid));

// db_query in Drupal 7 returns a stdClass object. 
// Value names are corresponding to the fields in your SQL query 
//(in our case "t.name") AND t.tid for build path.
$tags '';
foreach (
$term as $record) {
  
// I put l() function for make my links.
  
$tags .= l($record->name'taxonomy/term/' $record->tid) . ', ';
}

print 
'Tags: ' $tags;
?>

I hope that it helps you friends.

Thanks for sharing and help me out here with this handy post!

Thanks for the snippet @Mayk

Thanks for the snippet @Mark Brito!

So I guess the easiest way is to combine the two to print the tags with links?

<?php
// Load full $node object. IMPORTANT!
$node node_load($node->nid); 
// Get the reference id of the taxonomy term.
$tid $node->field_tags[$node->language][0]['tid']; 

// Query database table taxonomy_term_data with $tid from $node object.
$term db_query('SELECT n.name, n.tid FROM {taxonomy_term_data} n WHERE n.tid = :tid', array(':tid' => $tid));

// db_query in Drupal 7 returns a stdClass object. Value names are corresponding to the fields in your SQL query (in our case "n.name")

tag '';
foreach (
$term as $record) {
  
$tag .= l($record->name'taxonomy/term/' $record->tid) . ', ';
  print 
$tag;
}
?>

Haven't tested this snippet though. Can anyone please leave a comment if you get to try it out :P

Simpler Alternative

Just in case you didn't know, Drupal 7 loads the taxonomy object into the field for the node.

<?php
$term 
$node->field_tags[$node->language][0]['taxonomy_term'];
?>

However, Drupal 7 gives you the option to control how your term reference field will be output. So if you wanted your term to be output as a link to the term, go to 'manage display' for your content type, and choose 'linked' as your display format for the term. Then use the following code to display it.

<?php
print render($content['field_tags']);
?>

Also, if you are only expecting one result back from a db_query, you can do the following to just obtain your result as an object.

<?php
$term 
db_query('SELECT n.name, n.tid FROM {taxonomy_term_data} n WHERE n.tid = :tid', array(':tid' => $tid))->fetchObject();
?>

發表新回應

這個欄位的內容會保密,不會公開顯示。
  • 自動將網址與電子郵件位址轉變為連結。
  • 可使用的 HTML 標籤:<a> <em> <strong> <cite> <code> <ul> <ol> <li> <dl> <dt> <dd>
  • 自動斷行和分段。
  • You can enable syntax highlighting of source code with the following tags: <code>, <blockcode>, <c>, <cpp>, <drupal5>, <drupal6>, <java>, <javascript>, <php>, <python>, <ruby>. The supported tag styles are: <foo>, [foo], [[foo]]. PHP source code can also be enclosed in <?php ... ?> or <% ... %>.
  • Images can be added to this post.

更多關於格式選項的資訊