lele_ballack Posted July 28, 2020 Share Posted July 28, 2020 Hi everyone, I'm trying to build an index with all the fields' name that belong to a template, excluding those fields that are empty. <?php $i=0; foreach ($page->fields->find('limit=3') as $field) { ++$i;?> <li> <a class="list-item" href="#par-0<?php echo $i;?>" title="<?=$field->name?>"><?=$field->name?></a> </li> <?php } ?> Are there ways to do it in the selector? Thank you ? Link to comment Share on other sites More sharing options...
Robin S Posted July 28, 2020 Share Posted July 28, 2020 6 hours ago, lele_ballack said: Are there ways to do it in the selector? No, because you are finding Field objects these are neither empty nor not empty. Instead you need to check the values of the field names for the current page to see which are empty. Something like this... $i = 1; foreach($page->fields as $field) { // Limit to 3 non-empty field values if($i > 3) break; // Check if value is empty $value = $page->getFormatted($field->name); if($value instanceof WireArray) { if(!$value->count) continue; } else { if(!$value) continue; } // Do whatever with $field here echo "{$field->name}<br>"; $i++; } 4 Link to comment Share on other sites More sharing options...
lele_ballack Posted July 29, 2020 Author Share Posted July 29, 2020 Thank you so much @Robin S! Your solution works really well! 1 Link to comment Share on other sites More sharing options...
Recommended Posts
Create an account or sign in to comment
You need to be a member in order to leave a comment
Create an account
Sign up for a new account in our community. It's easy!
Register a new accountSign in
Already have an account? Sign in here.
Sign In Now