1

I wanted to print taxonomy terms on my custom node-mytype.tpl.php but what I want is not to print all the terms as they are.

Each of these terms consists of 2-3 words. And I want to manipulate the terms as below:

term1: word1 word2 word3

I want to print this as:

word3, word1 word2


If a node has one term it's easy and I can do this as below (of course there can be some other better way to do this):

inside node-mytype.tpl.php:

<?php if (!empty($node->field_my_taxonomy_term)) { ?> <?php $aut = ($node->field_my_taxonomy_term['und']['0']['taxonomy_term']->name); ?> <?php $exp = explode(" ", $aut); ?> <?php if (count($exp) == 2) {?> <?php print ($exp[1]); print t(", "); print ($exp[0]); print t(", "); ?> <?php } elseif (count($exp) == 3) {?> <?php print ($exp[2]); print t(", "); print ($exp[0]); print t(" "); print ($exp[1]); print t(", "); ?> <?php }?> 

I want to print multiple terms as above. For example:

term1: word1 word2 word3

term2: word4 word5 word6

should be printed as that:

word3, word1 word2 - word6, word4 word5


I have this:

 <?php $str = ''; ?> <?php if (isset($node->field_my_taxonomy_term[LANGUAGE_NONE]) && $node->field_my_taxonomy_term[LANGUAGE_NONE]): ?> <?php foreach ($node->field_my_taxonomy_term[LANGUAGE_NONE] as $obj): ?> <?php $str .= l($obj['taxonomy_term']->name, 'taxonomy/term/' . $obj['tid']) . '&nbsp;-&nbsp;'; ?> <?php endforeach ?> <?php print trim($str, '-&nbsp;'); ?> <?php endif ?> 


The below code prints the terms as that:

word1 word2 word3 - word4 word5 word6


What code should I use here to print the terms as I wanted?

word3, word1 word2 - word6, word4 word5

(Drupal 7.34)

2 Answers 2

1

Finally I did it as shown below (thanks SO):

 <?php if (isset($node->field_my_taxonomy_term[LANGUAGE_NONE]) && $node->field_my_taxonomy_term[LANGUAGE_NONE]): ?> <?php $allOf=''?> <?php foreach ($node->field_my_taxonomy_term[LANGUAGE_NONE] as $obj): ?> <?php $exp = explode (" ", $obj['taxonomy_term']->name); $exp_last = array_pop($exp); $eachOne = $exp_last .', ' .implode(' ', $exp); $allOf .= $eachOne . '&nbsp;-&nbsp;'; ?> <?php endforeach; ?> <?php print trim($allOf, '&nbsp;-&nbsp;'); ?> <?php endif ?> 

This is working correctly.

0
<?php $str = ''; $terms = array();?> <?php if (isset($node->field_my_taxonomy_term[LANGUAGE_NONE]) && $node->field_my_taxonomy_term[LANGUAGE_NONE]): ?> <?php foreach ($node->field_my_taxonomy_term[LANGUAGE_NONE] as $obj): ?> <?php $terms[] = $obj;?> <?php endforeach ?> <?php $str .= l($term[2]['taxonomy_term']->name, 'taxonomy/term/' . $term[2]['tid']) . ', '; $str .= l($term[0]['taxonomy_term']->name, 'taxonomy/term/' . $term[0]['tid']) . ' '; $str .= l($term[1]['taxonomy_term']->name, 'taxonomy/term/' . $term[1]['tid']) . ' - '; $str .= l($term[5]['taxonomy_term']->name, 'taxonomy/term/' . $term[5]['tid']) . ', '; $str .= l($term[3]['taxonomy_term']->name, 'taxonomy/term/' . $term[3]['tid']) . ' '; $str .= l($term[4]['taxonomy_term']->name, 'taxonomy/term/' . $term[4]['tid']) . ' '; ?> <?php print trim($str, '-&nbsp;'); ?> <?php endif ?> 
4
  • Thanks but it's not I wanted and it gives a lot of error. I think you misunderstood the question because the code you wrote seems to separate the terms not the words inside the terms. Not all nodes have 2 terms and not all terms have 3 words as I said above. I should do the manipulation for each term separately without knowing have many terms there are in a node. A node can have 1-10 terms and each term can have 2-3 words. Node has one 2-words term: Word2, Word1 Node has one 2-words term and one 3-words term: Word2, Word1 - Word5, Word3, Word4 Commented Nov 22, 2014 at 9:53
  • The taxonomy vocabulary is author names and on node-mytype.tpl.php I want to print the author names as that: Surname, Name MiddleName - Surname, Name MiddleName - ... Commented Nov 22, 2014 at 9:57
  • @herci what happens when your author's surname has more than one word eg. de Beauvoir, van der Velde, le Blanc etc? Commented Nov 22, 2014 at 13:39
  • @murraybiscuit, in the case the surnames are always one word. Commented Nov 22, 2014 at 13:51

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.