Drupal 7: View theming - Get specific field by it's number?
-
09-02-2021 - |
Question
I'm trying to get a specific field number in my "views-view-fields--news.tpl.php". Right now it's like this :
<?php foreach ($fields as $id => $field): ?>
<?php print $field->content; ?>
<?php endforeach; ?>
I've tried to do something like this :
<?php print $fields[0]->content; ?>
But it doesn't seem to works and Google doesn't care about my problem.
Can you help me figure this?
Solution
You have to use the field name instead. Try to use
<?php var_dump(array_keys($fields)); ?>
to find the one you need
OTHER TIPS
This is my individual field tpl file name in theme folder
views-view-field--field-fba-value.tpl.php
where "field-fba-value" is my field name. so don't need to use $field variable here and iterate it through loops. I just use $output variable that contain the current value of the field.
Draw back of this approach is if you want to put styling of each field then you need to create those many tpl file. But if you have one or two field to make styling on it then go with this approach.