Another taxonomy term list for Drupal

June 15th, 2007 tela

In my previous post I exposed one method how to make a list of the taxonomy terms with Drupal. This method was using the Taxonomy Menu module. However this method has some limitations.

One is that it creates specific URL for the menu item link. It doesn’t use the existing URL for the taxonomy term: /taxonomy/term/[term-ID]

It links to a page with a URL that looks like this: /taxonomy_menu/[vocabulary-ID]/[term-ID]

This can be a problem for example when you want to use a module like Pathauto to automatically generate URL aliases. The pages created by the Taxonomy Menu module are not managed by the Pathauto module. So it you want to keep a coherent naming system for your URLs you will need to change manually the URL aliases for these pages.

There is another limitation regarding its “SEO friendliness”. I think it’s always a good thing to use the title attribute in the <a> tag. This feature doesn’t come standard with Taxonomy Menu module. So if you want it, you need to manually edit each menu item to add a description.

Considering these limitations, and a process that is somehow quite complex, I have found another method which involves the use of a php block snippet.

This is a one step method.

You just need to create a new block: go to Administer > Site building > Blocks and click Add block.

Fill in the different fields:

  • Block description: this is the text used on the Blocks overview page.
  • Block title: this is the title that is displayed on the website.
  • Block body: copy and paste the code below:
<?php
$vid = 1; /* <---- put correct vocabulary ID here */
$terms = taxonomy_get_tree($vid);
print "<ul>";
foreach ( $terms as $term ) {
print "<li>".
l($term->name,'taxonomy/term/'.$term->tid, array('title' => $term->name)).
"</li>";
} /* end foreach */
print "</ul>";
?>
  • Select the correct Input format under Block body: PHP code.

Save the block.

On the Blocks overview page, locate the newly created block under Disabled. Select where you want to display your block from the dropdown list.

You’re done.

You can see this in action in the last website we created with Drupal: Tela-Group.com

Entry Filed under: Drupal

14 Comments Add your own

Pages: « 2 [1]

  • 10. rajasekaran  |  January 11th, 2008 at 1:54 pm

    very very useful for this programe very thanks

  • 9. leandro  |  January 4th, 2008 at 7:45 pm

    thanks, this aproaches prett much what i wanted. here’s what i want to do: i installed the og galleries module, it creates a new vocabulary with the name of the group, and terms in that vocabulary for each gallery, is there a way to make this block display the correspondent vocabulary for each group?
    i believe there shopuld be a way of doing this by adding some more code here: $vid = ” the vocabulary for current group”.
    i don’t have the apropiate php skills to do it, do you think this can be done? thanks, have a nice year

  • 8. How to print Taxonomy Voc&hellip  |  November 16th, 2007 at 8:02 pm

    […] find the information I needed anywhere in Drupal’s forum. But I was lucky enough to find it on this Tela-web.com. This is the code that saved the […]

  • 7. Adam Hegi  |  November 14th, 2007 at 7:57 pm

    many thanks! That worked great.

  • 6. Rob  |  November 13th, 2007 at 1:45 pm

    Hi,

    Thanks for the useful post. I’m linking to your article from my own site:

    http://webdeveloper.beforeseven.com/drupal/creating-block-list-taxonomy-terms

  • 5. xacro  |  October 10th, 2007 at 8:45 pm

    I googled that and found the function, taxonomy_term_count_nodes($tid)

    I’m not sure if it’s the proper way to do it, but it works for me!

    Here’s the modified block code:

    
    <?php
    $vid = 1; /* <---- put correct vocabulary ID here */
    $terms = taxonomy_get_tree($vid);
    print "<div class=\"item-list\">";
    print "<ul>";
    foreach ( $terms as $term ) {
    $tcount = taxonomy_term_count_nodes($term->tid);
    print "<li>".
    l($term->name." (".$tcount.")",'taxonomy/term/'.$term->tid, array('title' => $tcount." posts in ".$term->name)).
    "</li>";
    } /* end foreach */
    print "</ul>";
    print "</div>";
    ?>
    

    Thanks for the original code, tela!

  • 4. Chad  |  September 5th, 2007 at 5:40 pm

    What would be needed to count each node in the specific term?

    Example: Type of Animal
    Cat (43)
    Dog (321)
    etc?

  • 3. glen  |  July 24th, 2007 at 12:33 am

    Whoops, my pathauto wasn’t setup properly. When it is, it does give you the url aliases instead of the taxonomy/tid. Thanks

  • 2. glen  |  July 24th, 2007 at 12:27 am

    This works well enough if you want your path to be “taxonomy/term/something” but you made reference to pathauto not working well with taxonomy menu. How would you use pathauto in relation to your snippet?

  • 1. tom  |  July 11th, 2007 at 9:31 pm

    Thanks! This worked really well for me.

Pages: « 2 [1]

Leave a Comment

Required

Required, hidden

Some HTML allowed:
<a href="" title=""> <abbr title=""> <acronym title=""> <b> <blockquote cite=""> <code> <em> <i> <strike> <strong>

Trackback this post  |  Subscribe to the comments via RSS Feed