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']) . ' - '; ?> <?php endforeach ?> <?php print trim($str, '- '); ?> <?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)