2

How to print taxonomy terms as an un-ordered list in page.tpl.php. Where i want to print a taxonomy term called "Article Category" and all its vocabularies one by one as a un-ordered list, so it can be used as a menu. Can't understand, please give me ideas?

  • EDIT: I know about a module "Taxonomy Menu" but looking for a programmatic direction, idea or any kind of reference that will help me to get started.

Thanks

3
  • Hi rakibtg, are you looking to have the list display as a menu in-site, or just populate a php array to use however you want??
    – KyleM
    Nov 5 '13 at 7:10
  • Hey rakibtg, do you want to display taxonomy terms as menu to your site Nov 5 '13 at 7:24
  • @KyleM i want to print it as a list to use it as a menu. Thanks
    – rakibtg
    Nov 5 '13 at 7:45
2

OK, there is a module for that! Taxonomy Menu. Once installed, you can quite simply turn your vocabulary into a menu by going to admin > structure > taxonomy > {your vocabulary} > edit

You can also play around with it and get multi-level menus, which I think is what you are after/asking for.

Hope this helps!

2
  • That module seems to be great, but i was looking for a programmatic solution. Thanks for that :)
    – rakibtg
    Nov 5 '13 at 10:06
  • Ah sorry for that, how about reverse engineering the code from that Module and using that??
    – KyleM
    Nov 5 '13 at 11:23
1

I pulled this from here:

$terms = taxonomy_get_tree(1); // Use the correct vocabulary id.

// Get the active trail tid-s.
$active = arg(2);
$active_parents = taxonomy_get_parents_all($active);
$active_parents_tids = array();
foreach ($active_parents as $parent) {
  $active_parents_tids[] = $parent->tid;
}

// Build the menu.
$term_count = count($terms);
$cont = '<ul class="taxonomy_menu">';
for ($i = 0; $i < $term_count; $i++) {
  // Build the classes string.
  $classes = '';
  $children = taxonomy_get_children($terms[$i]->tid);
  $active_trail = in_array($terms[$i]->tid, $active_parents_tids);
  if ($active_trail && $children) $classes .= 'expanded active-trail ';
  elseif ($active_trail) $classes .= 'active-trail ';
  elseif ($children) $classes .= 'collapsed ';

  if ($i == 0) $cont .= '<li class="first '.$classes.'">'.l($terms[$i]->name, 'taxonomy/term/'.$terms[$i]->tid);
  else {
    if ($terms[$i]->depth == $depth) $cont .= '</li><li class="'.$classes.'">'.l($terms[$i]->name, 'taxonomy/term/'.$terms[$i]->tid);
    elseif ($terms[$i]->depth > $depth) $cont .= '<ul class="level-'.$terms[$i]->depth.'"><li class="first '.$classes.'">'.l($terms[$i]->name, 'taxonomy/term/'.$terms[$i]->tid);
    elseif ($terms[$i]->depth < $depth) {
      // Add missing end-tags depending of depth level difference.
      for ($j = $terms[$i]->depth; $j < $depth; $j++) {
        $cont .= '</li></ul>';
      }
      $cont .= '</li><li class="'.$classes.'">'.l($terms[$i]->name, 'taxonomy/term/'.$terms[$i]->tid);
    }
    // If we have reached the last element add all possibly missing end-tags.
    if (!isset($terms[$i+1])) {
      for ($j = 0; $j < $terms[$i]->depth; $j++) {
        $cont .= '</li></ul>';
      }
    }
  }
  $depth = $terms[$i]->depth;
}
$cont .= '</li></ul>';

Finding the $vid can be a bit tricky, start with 1 and go from there.

1
  • Things are now started :) let you know, my updates ... Thanks
    – rakibtg
    Nov 5 '13 at 19:38
1

You can try https://drupal.org/project/taxonomy_menu. This module helps you print any taxonomy vocabulary as menu. More details from the module's project page:

  • Custom Menu per vocabulary
  • Uses the default taxonomy term path or custom paths
  • Integrate with Views
  • Integrate with Pathauto: use URL/path aliases
  • Does not interfere with other menu modules
0
0

I think Views is the right and the best way to go. There are lots of tutorials on net about this; but if you need help please ask.

0

If I understood correctly, you are looking to print the taxonomy term for the page, below is the code you can place it in your page.tpl.

<?php   
foreach($node->taxonomy as $tid => $taxo)
   $taxo_links[] = l($taxo->name,"taxonomy/term/$taxo->tid", array('title' => $taxo->name));
print t('Located : '). implode('  >  ',$taxo_links);
 ?>

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.