Hi,

As we know drupal just lumps all taxonomies into one pile, and says print $terms. What I want to achieve for node.tpl.php is so that the Categories are displayed above the content, and tags are displayed below links only in full node view.

If I understand it correctly, there is no way to do this directly in node.tpl.php, so in template.php we need to define which $vid is Categories and which ones are Tags , and then pass it to node.tpl.php and simply print $categories and print $tags. But I can't find any correct snippet that will differentiate and define which vocabulary is $categories and which one is $tags inside template.php.

So far I found few similar solutions:
http://gerardmcgarry.com/blog/drupal-get-a-comma-separated-list-categori...
http://www.kobashicomputing.com/creating-a-comma-delimited-taxonomy-list...
http://drupal.org/node/38768 (but only for 5.x)

... but none of them

so far in my node.tpl.php I've got:


<?php if ($terms) { ?><div class="taxonomy"><?php print $terms ?></div><?php } ?>  //This is where I need to show only Categories and no tags.  Would be lovely if we could just say print $categories

<?php print $content ?>

<?php if (arg(0) == 'node'): ?>  //  Because I want the Tags to display only in Full view of the node

<!--  <?php
$term_links = array();
// "implode" makes the terms comma-separated
foreach ($node->taxonomy as $term) {
$term_links[] = l($term->name, 'taxonomy/term/' . $term->tid);
}
print implode(', ', $term_links);
?>
-->

<?php if ($terms) { ?>
  <div class="tags">
    <span><?php print t('Tags'); ?>:</span> <?php print $node_terms; ?>
  </div>
<?php } ?>    // Here I need the  

  <?php endif; ?>

Someone, please help with defining and differentiating the $categories from $tags inside the template.php
Or is there a better way to achieve this?

Comments

jix_’s picture

In template.php, add this:

<?php
function YOURTHEME_preprocess_node(&$vars) {
  // Seperate categories and tags.
  $vars['categories'] = '';
  $vars['tags'] = '';
  if (count($vars['node']->taxonomy) > 0) {
    $categories = array();
    $tags = array();
    foreach ($vars['node']->taxonomy as $term) {
      switch ($term->vid) {
        case 1: // Replace this with the id of your 'categories' vocabulary.
          $categories[$term->tid] = array(
            'href' => 'taxonomy/term/' . $term->tid,
            'title' => $term->name,
          );
          break;
        case 2: // Replace this with the id of your 'tags' vocabulary.
          $tags[] = l($term->name, 'taxonomy/term/' . $term->tid);
          break;
      }
    }
    $vars['categories'] = theme('links', $categories, array('class' => 'links categories'));
    $vars['tags'] = implode(', ', $tags);
  }
}
?>

Then, your node.tpl.php could be simplified to this:

<?php if (!empty($categories)): ?>
  <?php print $categories; ?>
<?php endif; ?>

<?php print $content ?>

<?php if ($page && !empty($tags)): ?>
  <div class="tags">
    <span><?php print t('Tags'); ?>:</span>
    <?php print $tags; ?>
  </div>
<?php endif; ?>
drupalina’s picture

Thanks mverbaar for taking time to reply and help.

I used the code you gave me, but it gave out "Tags: Array", and the categories are not displaying at all.

Indeed my Categories vid is 1 and Tags is 2, so I didn't change those. In my theme (newswire) template.php there is already a function function newswire_preprocess_node(&$vars, $hook) { so I just inserted this code before the last } of that function.

  // Seperate categories and tags.
  $vars['categories'] = '';
  $vars['tags'] = '';
  if (count($vars['node']->taxonomy) > 0) {
    $categories = array();
    $tags = array();
    foreach ($vars['node']->taxonomy as $term) {
      switch ($term->vid) {
        case 1: // Replace this with the id of your 'categories' vocabulary.
          $categories[$term->tid] = array(
            'href' => 'taxonomy/term/' . $term->tid,
            'title' => $term->name,
          );
          break;
        case 2: // Replace this with the id of your 'tags' vocabulary.
          $tags[] = l($term->name, 'taxonomy/term/' . $term->tid);
          break;
      }
    }
    $vars['categories'] = theme('links', $categories, array('class' => 'links categories'));
    $vars['tags'] = implode(', ', $tags);
  }

in my node-story.tpl.php I inserted the codes you gave me, but no Categories are being displayed and where the tags should display, it says "Tags: Array"

Any idea what I did wrong?

Thanks again!

jix_’s picture

I just tested the code and it worked here. (Printing categories as ul + lis and tags comma-seperated.)

Can't really think of anything that could be the problem at the moment ...

Perhaps put a drupal_rebuild_theme_registry(); at the top of template.php while developing, just in case. Although I'm pretty sure that can't be it ...