Syndicate

Feed

Putting some order in your terms

In Wordpress, unlike in Drupal, terms are not lumped together in posts. Each Wordpress vocabulary has its own “template tag”, and the ones that come out-of-the box are: the_tags(), and the_category(). The following theming tweak is about putting order in Drupal terms before they're output to screen. It you need to break up your terms by vocabulary before you display them, read on.

Here's a Wordpress blog entry:

Terms presented by vocabularies in Wordpress

I will present an all-purpose solution that will print all terms by vocabulary. Each vocabulary list will be wrapped in its own HTML element, and I will use the vocabulary name as a 'label' for each list — a label you will be able to edit in the Administration Section of your Drupal site.

Solution

  1. Edit template.php to rebuild your $node->taxonomy array...

  2. ... and re-theme that array into a new $terms variable... and then...

  3. style your terms as needed in style.css.

Say you have two vocabularies, one for “free tagging”, and another for filing posts under sections, like so:

My vocabularies listed

You may not like your free tags to be lumped together with your “Filed under” terms.

You may prefer to see something like this:

Terms presented by vocabularies in Drupal

Let's get to it.

You will use a prepocess function for your node template. You will add this function to template.php if it has not already been defined. Open your theme template.php file in a text editor, and add the following code (please read the comments):

/**
* Override or insert PHPTemplate variables into the node template.
*/
function phptemplate_preprocess_node(&$vars) {
  // If we have any terms...
  if ($vars['node']->taxonomy) {
    // Let's iterate through each term.
    foreach ($vars['node']->taxonomy as $term) {
      // We will build a new array where there will be as many
      // nested arrays as there are vocabularies
      // The key for each nested array is the vocabulary ID.     
      $vocabulary[$term->vid]['taxonomy_term_'. $term->tid]  = array(
        'title' => $term->name,
        'href' => taxonomy_term_path($term),
        'attributes' => array(
          'rel' => 'tag', 
          'title' => strip_tags($term->description),
        ),
      );       
    }
    // Making sure vocabularies appear in the same order.
    ksort($vocabulary, SORT_NUMERIC);
    // We will get rid of the old $terms variable.
    unset($vars['terms']);
    // And build a new $terms.
    foreach ($vocabulary as $vid => $terms) {
      // Getting the name of the vocabulary.
      $name = taxonomy_vocabulary_load($vid)->name;
      // Using the theme('links', ...) function to theme terms list.
      $terms = theme('links', $terms, array('class' => 'links inline'));
      // Wrapping the terms list.
      $vars['terms'] .= '<div class="vocabulary taxonomy_vid_';
      $vars['terms'] .= $vid;
      $vars['terms'] .= '">';
      $vars['terms'] .= $name;
      $vars['terms'] .= ':&nbsp;';
      $vars['terms'] .= $terms;
      $vars['terms'] .= '</div>';
    }
  }    
}

Here is what the preprocess function does essentially:

Overwriting the $terms variable with a preprocess function.

The new HTML generated from print $terms (in node.tpl.php) is shown in this Firebug screen capture:

Terms are now printed by vocabularies


Creating PHPTemplate variables to pass on to your node.tpl.php template

As Opusoid recommends in a comment below, you can add each vocabulary to a new variable, that you pass on to your node template. In node.tpl.php, you can output each vocabulary variable wherever you wish. It gets super easy to place each vocabulary list in a precise location in the node: above it, below it, wherever you want. Brilliant, Opusoid, thank you!


Ordering vocabularies by weight — Improved version by manuee

Manuee modified the code snippet above to order vocabularies by weight. This is something you'll most likely prefer to do. After all, that's what weight is all about: presentation order. His code snippet is in his comment below. Thanks manuee!


Does that work in Drupal 5?

This solution will work in Drupal 6 only. Of course, there's an equivalent method for Drupal 5, and if someone asks for it I will provide it.

In Drupal 5, there was a function called taxonomy_get_vocabulary($vid) .This function has been renamed in Drupal 6 to taxonomy_vocabulary_load($vid). It has been renamed probably to bring naming consistency between functions that load objects, such as node_load() and user_load(). The function returns the vocabulary object matching the vocabulary ID $vid. The vocabulary object contains 'name' and 'description' properties, both of which you can use in your theme.


CSS styling

You may need to style your terms if you want them to appear on the same line, like so (these are rules added to the Garland theme style.css file):

/**
 * Terms styling rules
 */
 
.vocabulary {
  display: inline-block;
  padding-right: 1.5em;
}
 
.terms {
  float: none;
}
Last edited by Caroline Schnapp about 12 years ago.

Comments

thank for this

Great efforts and great idea that you put into this blog. Thanks for the information. Very usefull

Thats helps a lot

thx for sharing this. I still learn css & html .. but this is great for new php sites!

More then one vocabulary

I've see the great suggestion of Opusoid for separate vocabulary terms:

$vars['tags']= theme('links',$vocabulary[4], array('class'=>'links inline'));
$vars['categories']= theme('links',$vocabulary[3], array('class'=>'links inline'));

But ... if I want to add more then one (3 in this example) vocabulary (or exclude the free tagging, 4 in this example)?

My intention is to have one vars for tags and one vars for the others

Thanks for the article

It is absolutely amazing!

Thanks you a lot!

I can't get Opusoid's idea

I can't get Opusoid's idea to work; where to edit the code for his idea to work?

Hide certain tags?

Is there a way to hide specific tags from being displayed in a node?

Say a review for Star Wars has these tags:
sidebar, Star Wars, Darth Vader, Luke Sky Walker

The "sidebar" tag is used in views to place the title in the site sidebar, but I wouldn't want that displayed as a term on the page.

Is there a way to do that/restrict certain terms from being displayed?

Thanks!

How to display tags in the order that the user had entered them?

This is very interesting. But what if we want to display the tags in that order in which the end-user had entered them?

Right now Drupal will display Term1, Term2, Term3 alphabetically. But what if the Term2 is more pertinent to a given content? Surely, it would make more sense to display the tags in the order that the user had entered them. For example, if the user entered: "Term2, Term3, Term1" -- then on the node page it should also output as "Term2, Term3, Term1"

Any idea how to achieve that minor (but vital) improvement?

Views ?!

@Blog author

Great work! I wonder if the suggested code would also work and format the way terms are displayed inside a custom view ?!

Can you separate the Category and Tags. . .

Thank you for sharing this, I was able to copy and paste the code and it worked perfectly! I was wondering, however, if there is a way to separate the categories and the tags into separate variables, to permit, say, showing the category on top near the title and the tags on the bottom.

I am trying to think it through myself, but am still new to Drupal and PHP, so any suggestions would be greatly appreciated.

Thank you again for sharing this and making it available to the community!

Use alias url paths for taxonomy links

Even though it's a rather old post and Drupal 7 is already out, I've to maintain several D6 sites and just came across this problem. Your solution works like a charm.

I've made a little addition, though, to use alias paths:

'href' => drupal_get_path_alias(taxonomy_term_path($term), $node->language),

Incomparable post

Incomparable and high class content write up. Thanks for aware and give us great information.
Mehul Travels | Manali Travels

nice blog

A perfect info source. Thanks for taking the time to discuss this, I feel strongly about it and love learning more on this topic

nice

You've nice and straightforward interface together with fantastic stuff on your blog. I also manage a personal blog and now you are in my favorite blog list. thanks for the wonderful aid and have a nice day. Bank Exams

hello!! Very interesting

hello!! Very interesting discussion glad that I came across such informative post. Keep up the good work friend. Glad to be part of your net community.
can am spyder frame

This is a great inspiring

This is a great inspiring article.I am pretty much pleased with your good work.You put really very helpful information...
cool iPod Touch apps

Explanation

Thorough explanation - thanks for that.

Great post

Great article. Thanks for sharing this. But can we use it as well in Dupral 5?

Worth Bookmarking

Sant Ritz facilities provide full family entertainment needs for your family and loved ones. Indulge in a serene and tranquil lifestyle right in the heart of Potong Pasir.

Good thoughts and well-written

Several buses are available near Twin Fountains EC along with shopping centers and restaurants. Twin Fountains EC is also near Causeway Point as well as Woodlands Waterfront. Entertainment for your loved ones and friends are therefore at your fingertips with the full condo facilities as well as the amenities near Twin Fountains EC.
Woodlands EC

For Bartley Ridge vehicle

For Bartley Ridge vehicle owners, it takes less than 10 minutes to drive to the business hub and vibrant Orchard Road shopping district, via Pan Island Expressway (PIE) and Central Expressway (CTE).
Bartley Ridge

'm new at drupal and I've

'm new at drupal and I've tried to add your phptemplate_preprocess_node funtion in my theme template.php file, but afterwards it takes no effect. The e-papierosy themes variable remains the same in node.tpl.php. I've also tried to create a new variable $vars['myterms'] but it isn't passed to node.tpl.php.

You may use taxonomy for

You may use taxonomy for organizing e-papierosy your content, but may wish to NOT e-papierosy display terms that belong to certain vocabularies in your node. With the solution I presented, it becomes very easy to do so.

excellent post

Belgravia Villas has full and unique facilities, which includes a guard house, clubhouse, children's playground, swimming pool, Aerobic/Yoga room, piano room, pool room, indoor gym, hydrotherapy beds, hydrotherapy baths, reading room, function room, onsen, jacuzzi.
Belgravia Villas

Great post!

Nice post. I've been using Pinterest more and more to showcase my own work but also as a way to help brides gather ideas.

excellent

Great blog. hope to read more from author. Hedges Park Condo, Topiary EC, Twin Fountains Woodlands

Great Post. I have not been

Great Post. I have not been visiting the site recently. Took a visit again and there were some great comments on the site. Excellent post. Keep up the good work.
Ecopolitan

Jewel at Buangkok is a new

Jewel at Buangkok is a new and upcoming condo located in Buangkok Drive and Sengkang Central area, within a short walk to Hougang Green Shopping Mall and a short drive to Compass Point. With expected completion in mid 2016, it comprises of TBA towers with TBA units and stands TBA storeys tall.
Jewel at Buangkok

J Gateway is a new and

J Gateway is a new and upcoming condominium located in Boon Lay Way, Jurong East area. It is located right beside JCube, and the upcoming Westgate and Jem. With expected completion in mid 2016, it comprises of 4 towers with 783 units and stands 38 storeys tall.
J Gateway

Coral Edge Residences has

Coral Edge Residences has full and unique facilities, which includes a guard house, clubhouse, Function Room & Indoor Gym Tennis Court, 50m Freeform Pool Pool Deck, Wading Pool, Splash Pool & Family Pool Jacuzzi & Hydro Spa, BBQ Area Dining and Play Fountain, Fitness Alcove & Children’s Playground and Garden Trail. The condo’s facilities provide full family entertainment needs for your family and loved ones. Indulge in a serene and tranquil lifestyle right in the heart of Punggol.
Thx
Coral Edge Residences

Future residents will be

Future residents will be able to walk to the existing Bugis MRT. With such a short drive to the city area as well as the orchard and bugis area, entertainment for your love ones and family will come at a stone’s throw away.
DUO Residences

The Panorama will be

The Panorama will be accessible via public transport along Ang Mo Kio Ave 5. Commuting to Toa Payoh and Paya Lebar area as well as the city area is therefore very convenient. It is also near to many eateries along the Upper Serangoon area as well as NEX shopping mall. The Panorama

The Inflora Condo is also

The Inflora Condo is also near elite schools such as Japanese Primary School, Dunman Secondary School, Ngee Ann Secondary School and Temasek Junior College. Tampines Junior College, Temasek Polytechnic and United World College (East Campus) is also around in the area. Inflora Condo

Future residents are within

Future residents are within walking distance to Bishan Junction 8 and a short drive to Ang Mo Kio Hub from Sky Vue Condo. With such a short drive to the city area as well as the orchard and bugis area, entertainment for your love ones and family will come at a stone’s throw away from Sky Vue Condo Sky Vue

Jurong West new Condo Launch by MCL And

Jurong West Condo is also a short drive to Pan Island Expressway(PIE). The development is also near to Chinese Garden, Japanese Garden and many shopping centres and entertainment outlets at Jurong East such as Superbowl Jurong and Snowcity. Jurong West Condo mrt distance

Dog Grooming esentially

Dog Grooming esentially refers to maintaining the hygiene and cleaning of a dog. Dog Grooming is necessary as it is important in keeping the dog comfortable and healthy. Dog Grooming

I read this site with

I read this site with carefully & attentively, this site is very informative and helpful for all of
the people. And whose people are wants to get more information. AC Maintenance Contract

Boardwalk Residences in Sengkang Fernvale Close

Boardwalk Residences has full and unique facilities, which includes a guard house, clubhouse, Function Room & Indoor Gym Tennis Court, 50m Freeform Pool Pool Deck, Wading Pool, Splash Pool & Family Pool Jacuzzi & Hydro Spa, BBQ Area Dining and Play Fountain, Fitness Alcove & Children’s Playground and Garden Trail. Boardwalk Residences in fernvale close

Pet Grooming Centres in Singapore

Dental care is one of the most important and difficult part of dog grooming singapore as the pet dog may feel uncomfortable and turn agreesive when the owner or pet groomer attempts to brush the teeth.Pet Grooming

Lakeville is a new and

Lakeville is a new and upcoming condominium located in Boon Lay Way, Jurong East area. It is located right beside JCube, and the upcoming Westgate and Jem. With expected completion in mid 2016, it comprises of 4 towers with TBA units and stands TBA storeys tall. Lakeville Condo Jurong East Condo

Lakeville is a new and

Lakeville is a new and upcoming condominium located in Boon Lay Way, Jurong East area. It is located right beside JCube, and the upcoming Westgate and Jem. With expected completion in mid 2016, it comprises of 4 towers with TBA units and stands TBA storeys tall. Lakeville Condo Jurong East Condo

Lakeville is a new and

Lakeville is a new and upcoming condominium located in Boon Lay Way, Jurong East area. It is located right beside JCube, and the upcoming Westgate and Jem. With expected completion in mid 2016, it comprises of 4 towers with TBA units and stands TBA storeys tall. Lakeville Condo Jurong East Condo

Highline Residences is a new

Highline Residences is a new and upcoming condominium located in Tiong Bahru, along Kim Tian Road. It is located right beside Tiong Bahru Plaza and Central Plaza. With expected completion in mid 2017, it comprises of TBA towers with TBA units and stands TBA storeys tall. http://highlineresidence.net

The Santorini is a new and

The Santorini is a new and upcoming condo located in Tampines Ave 10 and Bedok Reservoir area, within a short walk to Bedok Reservoir and a short drive Century Square. new condos

The Interlace has full and

The Interlace has full and unique facilities, which includes a guard house, clubhouse, Function Room & Indoor Gym Tennis Court, 50m Freeform Pool Pool Deck, Wading Pool, Splash Pool & Family Pool Jacuzzi & Hydro Spa, BBQ Area Dining and Play Fountain, Fitness Alcove & Children’s Playground and Garden Trail. The Interlace at jurong

your post is providing some

your post is providing some beneficial information to the viewer.. Build Biceps without Weights