Reputation: 43
For variuous reasons, I am using taxonomy terms for authors. Each blog node has a term reference field - field_authors - which lists one or more authors. What I would like to do is display the term description for each author listed in the page. My searches online got me the following code which I placed in node--blog.tpl.php just after the title of the blog post.
<?php
$vid = taxonomy_vocabulary_machine_name_load("authors")->vid;
$terms = taxonomy_get_tree($vid, 0, null, true);
$term_count = count($terms);
for ($i = 0; $i < $term_count; $i++) {
$name = $terms[$i]->name;
$id = $terms[$i]->tid;
$description = $terms[$i]->description;
?>
<div id="<?php print $id; ?>" class="taxonomy-description">
<?php print $description; ?>
</div>
<?php } ?>
As you can probably tell, it prints all taxonomy term description for the vocabulary 'authors'. I would like to show just the descriptions for the terms(authors) listed in that page.
It also occurs to me that there maybe better ways of achieving this. So any better suggestions would be most appreciated.
Upvotes: 0
Views: 2191
Reputation: 2456
I would suggest use Views module and create a block that you will place above the node.
1) The view will be of type "Taxonomy term". Create a block only.
2) Add a Relationship with "Taxonomy Term: Content using Authors" so you will be able to join the node table with the taxonomy term table (in my case the vocabulary is Authors)
3) Add a Contextual filter "Content: Nid" for the nid of the node that is displayed. This comes into scope because of the previous relationship.
4) Configure the Filter settings accordingly. Set the "Provide default value -> Content ID from url"
5) Add the Term Description field or any other field that you need to display above the blog node.
6) Go to the Block management (admin/structure/block) and enable the new block to display above Content. You can also set this to appear only for blog content type but in any case, since we have the contextual filter for the nid this is not necessary.
7) Enjoy! This is an easy example of the power of views and Drupal!
Upvotes: 2