Community & Support

Displaying taxonomy terms with Contemplate

The variable $taxonomy is listed in contemplates for custom theming. However, it's an array. What's the php you need to use with contemplate.module to display taxonomy terms?

Comments

Bump...

I have the same question... lil' help please?

this could be a completely

this could be a completely stupid answer but did you go to admin >> categories >> yourcategory >> and apply it to the specific content type? If so then it should display.

http://chibabeat.com/
If you like my advice then please link to me! Believe me! I need the links!

Bump and a reply

I appreciate your taking the time to reply with a suggestion, but the issue runs deeper than that. When you use the contemplate module, you have the option of theming specific variables you want to display within the body of the node. Contemplate is an alternate way of theming from just using custom phptemplate files - it's quicker in many ways.

With contemplate, you can add the php:

<?php
print $body;
?>

to the custom body field for a specific node type. Doing so will print all the variables associated with the node - including taxonomy. However, it will also print the labels for custom CCK fields, as well as other fields you might not want to display - or that you might want to display in custom ways.

The latter is the case for most of my CCK node types.

So, my question is, what is the php that I need to insert into the body field of my custom contemplate template to get taxonony to appear within the body of a CCK node?

I know that $taxonomy is an array, so what's the function that prints its terms within the template? I also know that if I were to use a custom node.tpl.php file, I could just enter:

<?php
print $terms;
?>

But that doesn't seem to work here. I've also tried:

<?php
print theme(taxonomy, $node);
?>

But that doesn't work either.

Thanks for the help!

Cheers,
Sean

Sean Larkin
ThinkShout

Here's what worked for me...

For a display of taxonomy terms in a node view:

<div class="custom_taxonomy_node"><b>Categories:</b><ul>
<?php foreach ((array)$taxonomy as $item) { ?>
<li>
<a href="<?php print base_path() . "taxonomy/term/" . $item->tid; ?>"><?php print $item->name ?></a>
<?php } ?>
</ul>
</div>

You can style at will using a css class of "custom_taxonomy_node" (I use a separate version for node lists).

Thanks

This snippit is exactly what I needed. :)

Michelle

--------------------------------------
My site: http://shellmultimedia.com

-----
Shell Multimedia - My sporadically updated mostly Drupal blog.

listing terms by vocabulary

I am trying to do a very similar output, but I am using multiple taxonomies. I'd like to show terms arranged by taxonomy, one of which uses "multi, another only allow a single category, and i'd like to hide a third vocabulary that is used for internal tracking rather than user output.

The output would like something like:

Category: Graphic design
Tags: Business Cards, Color, Marketing

Can someone point me in the right direction?

Thanks.

-Nirad

I am playing with exactly that

This will filter for terms in the vocabulary with id "4" -- in my case "Participating artists":

<b>Participating Artists:</b>
    <?php foreach ((array)$taxonomy as $item) { ?>
    <?php if ($item->vid == 4): ?>
        <li>
        <a href="<?php print base_path() . "taxonomy/term/" . $item->tid; ?>"><?php print $item->name ?></a>
        </li>
    <?php endif; ?>
<?php } ?>

I bet you are looking for something slightly more complex, though. If you try

<?php
  print_r
($node->taxonomy);
?>
you can see the full array that is available for $node->taxonomy -- that should give you a bit more to work with. $node->taxonomy is a recursive associative array of taxonomy objects, which means you have to dig into PHP a little bit to tinker with it. In English, I think you want ...

If this node contains terms in the "category" vocabulary, print "Category:" and then print a list of those terms. If this node contains terms in the "tags" vocabulary, print "Tags:" and then print a list of those terms.

I am not sure what the right approach to this is, php-wise. You could call up a list of vocabularies and then walk through them, checking for terms in each vocabulary? Or you could define the few vocabularies that interest you and code each one.

Let us know what you come up with. I'm taking a break for a while, but I'll check back here when I return to this project.

Amanda Bee

This works great, only thing

This works great, only thing i am wondering is why in body variables list i dont see taxonomy array? there are only $node arrays and variables? (d6 version)

Works great for me, thanx!

Please help. Tell how to check whether the node is attached to the term in this snippet

<b>Participating Artists:</b>
    <?php foreach ((array)$taxonomy as $item) { ?>
    <?php if ($item->vid == 4): ?>

////<?php if(!empty(....$taxonomy.???)){
        <
li>
        <
a href="<?php print base_path() . "taxonomy/term/" . $item->tid; ?>
"><?php print $item->name ?></a>
        </li>
////}
////else {print 'no';}
    <?php endif; ?>

<?php } ?>

////// is my rough idea of the beautiful PHP that should be there

category name?

This was exactly what I was looking for as well, although I wanted to display it a little different, I wanted to display the category name on the side of the term name? anyone got any ideas? I was able to display the term description but not the category name.

Any help would be much appreciated!

Thanks

Edmond

One Solution, from the Handbook

this doesn't explain the missing pieces in Contemplate, but here are two good approaches to this problem:

* sort taxonony links ($terms) by vocabulary ($vid) or
* Display taxonomy terms broken out by vocabulary

Hope that helps. The latter is pretty solidly what Nirad asked for.

Amanda Bee

subscribe

this still works in drupal 6

subscribe

indeed, it still works in d6, but how would i need to change it in order to get the url alias of the taxonomy term?

Drupal 6 List terms by vocabulary

The solution to your problem is here:

http://drupal.org/node/133223#comment-1353692

Taxonomy Term Url Alias in Contemplate

Hi, been following this thread. I need to list the my vocabulary terms, (as solution previously provided) , but with the pathauto URL alias for the taxonomy vocabulary/term instead of the hardcoded: "taxonomy/term" as in the example provided.

<div class="custom_taxonomy_node"><b>Categories:</b><ul>
<?php foreach ((array)$taxonomy as $item) { ?>
<li>
<a href="<?php print base_path() . "taxonomy/term/" . $item->tid; ?>"><?php print $item->name ?></a>
<?php } ?>
</ul>
</div>

I did try to use the function : drupal_get_path_alias, but to no avail.

$path = base_path() . $item->tid;
$path = drupal_get_path_alias($path);?>

Any help much appreciated, thanks Hix

subscribing

subscribing

Brian Gilday
Aha Consulting, Inc.
www.ahaconsulting.com

Print taxonomy terms

Here's a page outlining methods for newer versions of Drupal: http://drupal.org/node/133223

Alex
----------
We Rock Your Web

nobody click here