Flutter or Fresh-pages is a really handy plugin for WordPress. With it, you can quite easily create custom write panels, fields and groups of information.
Having worked with this plugin quite extensively recently, I have encountered a few issues which have proved somewhat frustrating, dealing with duplicate groups or fields in particular. Flutter uses a counter based system in which to access duplicate groups. Simple enough when using a standard for loop and all the duplicates are in the correct order – but somewhat tricky to handle when say, deleting one of the duplicates.
I’ve since resorted to writing a function to make handling these duplicates and a number of associated problems whilst looping through them much, much simpler.
Which brings us to the following function – which you can paste straight into your theme’s functions.php file :
function jt_get_flutter_duplicates($field, $group){
if (is_string($group)){
$numfiles = getGroupDuplicates($group);
$isgroup = true;
} else {
$numfiles = getFieldDuplicates($field, $group);
$isgroup = false;
}
$return = false;
if ($numfiles > 0){
$return = array();
$count = 1;
$tmp = "";
$first = "";
$total = ($numfiles > 1) ? 100 : 1;
while($count <= $total){
if ($isgroup){
$value = get($field, $count, 1);
} else {
$value = get($field, $group, $count);
}
if (empty($value) || $value == $first || $tmp == $value){
$count++;
continue;
} elseif ($count == 1){
$first = $value;
}
$tmp = $value;
$return[] = ($isgroup) ? $count : $value;
}
}
return (is_array($return) && count($return) > 0) ? $return : false;
}
I’m not going to go into too much detail right now, but essentially what this function does is gets the required field or group duplicates, loops through them (up to a max of 100 iterations just to be safe) and places them into an array containing the correct index values (if a group) or an array of field values (if a field).
Group duplicates
Set the first parameter to the name of a field within the group which should contain a unique value (such as name, or caption). The second parameter can be any field within the required group.
$groupItems = jt_get_flutter_duplicates("news_image", "news_image");
if (is_array($groupItems) && count($groupItems) > 0){
foreach($groupItems as $i) {
echo get_image("news_image",$i, 1, 1);
echo "<br />" . get("news_image_caption",$i, 1, false);
}
}
Field Duplicates
Set the first parameter to the name of the field you want to retrieve. Set the second parameter to the group index you want to retrieve the field duplicates from. For instance, you could have 1 group which can be duplicated, containing 3 fields, one of which can be duplicated. When looping through the group duplicates, you’ll want another sub-loop to handle the duplicated field contents. The function will return an array of values which you can then use in a standard foreach loop.
$groupItems = jt_get_flutter_duplicates("office_title", "office_title");
if (is_array($groupItems) && count($groupItems) > 0){
foreach($groupItems as $i) {
echo get("office_title",$i,1,false);
$contacts = jt_get_flutter_duplicates("office_contact", $i);
if (is_array($contacts) && count($contacts) > 0) {
foreach($contacts as $contact){
echo "<br />Contact Person: " . $contact;
}
}
}
}
This is of course a quick fix and by no means infallible, so I’d love to hear if you have any improvements or a simpler way of dealing with this particular issue.