Apologies if this is stupidly simple, but I have unsuccessfully looked around for the answer.

I need to add a class to each value in a multiple value field.

I would like to wrap each taxonomy term link with the same Bootstrap class. 'Multiple field settings' offers options like Unordered List, Ordered List, etc. but does not allow wrapping a class around each of the multiple values; and adding a style to the field simply wraps the entire field (containing the multiple values) with a class—I want to style each individual value, not the whole block as one.

I've tried a few 'foreach' options in the various Views template files but reckon I'm missing the correct terms and php stuff to get it to work.

I want to split:

<?php print $fields['field_tags']->content; ?>

into its component values, styled individually.

Any help would be greatly appreciated.

Comments

andrew smith created an issue. See original summary.

andrew smith’s picture

Issue summary: View changes
Subhransu.addweb’s picture

For your purpose override view field template files.

$custom_output = '';
foreach ($row->field_field_tags as $key => $value) {
  $tid = $value['raw']['tid'];
  $term_name = $value['raw']['taxonomy_term']->name;
  $custom_output .= '<a 
href="/taxonomy/term/'.$tid.'" class="test_classs">'.$term_name.'</a>';
}
?>
<?php print $custom_output; ?>

Hope this works for you.

andrew smith’s picture

It did!

Now it's written out, I start to see how it works.

Brilliant. Thank-you very much.

renatog’s picture

Status: Active » Reviewed & tested by the community

Thank you @denishaaddweb for your help.

Best,

renatog’s picture

Status: Reviewed & tested by the community » Fixed

Status: Fixed » Closed (fixed)

Automatically closed - issue fixed for 2 weeks with no activity.