doc4design.com - Tutorial: Flutter Duplicate Fields
12. July 2009 · Author: Dale Crum
67 Comments

Tutorial: Flutter Duplicate Fields

Creating duplicate fields through Freshout’s WordPress plugin Flutter has been a little on the mysterious side up to this point. No documentation seems to exist for this feature with the exception of simply repeating a php get_post_meta tag for each Custom Field. The true beauty behind the duplicate fields lies within the ability to utilize a single php function to echo all fields as they are created on the fly.

An example:

I set up a Custom Write Panel Page titled “Upcoming Events” within this page is a group and the option for duplication is checked, let’s title this group “Event” and create a Custom Write Fields like so:

CUSTOM FIELD 1:
Name: event-title
Label: Event Title

CUSTOM FIELD 2:
Name: event-date
Label: Event Date

For example purposes let’s say I’m giving a speech in California on the 5th of July 2011. The first text field, “Event Title”, might be filled in as “IT Security World 2011″ and the second field might be a text field showing the date of “July 5th 2011″. Initially echoing theses fields is straightforward:

<?php echo get_post_meta($post->ID, 'event-title', $single=true) ?>
<?php echo get_post_meta($post->ID, 'event-date, $single=true) ?>

If echoing individual, duplicate fields within multiple groups:

<?php echo get('event-title', 1); ?> // Duplicate Group 1, Field 1
<?php echo get('event-title', 2); ?> // Duplicate Group 2, Field 1

The above code works perfectly until the time comes to add in a second or third event that we would like to automate. Keep in mind we are simplifying the situation and the above is assuming that you would not be archiving the events, but merely wanting to show the latest five or six at any given time. This is all well and good until you start clicking that duplicate button and you realize the second or third event doesn’t actually show because the new fields created must have their own individual names in order to exist, something such as “Event1″.

After looking through the Flutter documentation we find a few functions that seem as though they should deliver what we need. getFieldDuplicates ($fieldName, $groupIndex) and getFieldDuplicates ($fieldName, $groupIndex) unfortunately these only return true values and are not what we are really looking for. What we want is the ability to click the duplicate button and have a single function return the values from the first event group and the next event group and so on.

Not much help has been handed down through the Flutter forums and it seems if anyone does know how to work this they have been doing a great job of keeping it a secret up to this point. I found only one person willing to shed any kind of light on the matter and as grateful as I am this was still not enough. Finally through shear frustration I took it upon myself to solve the problem through php. Albert Yarusso was kind enough to assist me in this project. Below is the php function we compiled to display duplicated groups.

<?php $my_query = new WP_Query('page_id=1');
      while ($my_query->have_posts()) : $my_query->the_post();
      $do_not_duplicate = $post->ID; ?>

<?php $total = getGroupDuplicates('event-title');?>
<?php for($i = 1; $i < $total+1; $i++):?>
<?php echo "<div class='event-wrap'>";
      echo "<h2>" .get('event-title',$i,1). "</h2>";
      echo get('event-date',$i,1);
      echo "</div>"; ?>

<?php endfor;?>
<?php endwhile; ?>

Line 1-3: <?php $my_query = new WP_Query('page_id=1');
To begin we need to create a WordPress query using our page_id number where the duplicate Custom Fields are located. This could just as easily be a post id number with duplicate fields. The first three lines should be understood as a basic concept from WordPress. If these are not understood please refer to WordPress codex on Loops. NOTE: If using this code on a page such as single.php use the basic loop: <?php if ( have_posts() ) : while ( have_posts() ) : the_post(); ?>. The above example is necessary only when calling on a specific post or page.

Line 5: <?php $total = getGroupDuplicates('event-title');?>
NOTE: This is not the name of the entire group, but the name of the first duplicate field.
Gathering a total number of duplicates. In this case these are duplicate groups and not duplicate Custom Fields. This is because we have set the group to duplicate and not the individual fields.

Line 6: <?php for($i = 1; $i < $total+1; $i++):?>
Start a count for each duplicate group in an effort to process them and add them incrementally.

Line 7: <?php echo "<div class='event-wrap'>";
Insert a simple php echo to add a bit of CSS styling for our events. This is only included to demonstrate the ability to add in CSS styling. Any CSS styles are housed within the stylesheet.

Line 8: echo "<h2>" .get('event-title',$i,1). "</h2>";
Here we are inserting a CSS H2 style tag to the event title to give it a better look, then echo the event titles incrementally and end the H2 tag.

Line 9: echo get('event-date',$i,1);
Add the event date which again is done incrementally.

Line 10: echo "</div>"; ?>
It is then important to end the div tag which opened on line 7.

Line 12: <?php endfor;?>
Then end the “for” function.

Line 13: <?php endwhile; ?>
Lastly don’t forget to close up the WordPress query to finalize the loop.

Now when adding additional duplicate groups on the post or page the code recognizes that there is more than one group and subsequently echos them all. With a few tries you should have this up and running for any manner of duplicate groups or fields.

67 Comments

  1. zac,

    Untested but give this a try:

    <?php $total = getFieldDuplicates('mainImage');?>
    <?php for($i = 1; $i < $total+1; $i++):?>
    <?php echo get('mainImage',$i,1); ?>

  2. Hi there and thanks for writing this! This is the first site I have come across that explains anything and there is very little (NO?!) documentation on the flutter site for this great feature of the plugin. I am trying to use getFieldDuplicates to echo however many images are uploaded. It works fine if there are 2 or more images but nothing is shown if only one image is included. Can someone please help. Here is what I am trying to use to call the images…

    <?php
    $total = getFieldDuplicates('mainImage',1);
    for($i = 1; $i < $total+1; $i++){
    echo get('mainImage',1,$i);
    }?>

    Thanks for any help!

  3. Jesse,

    I was just about to suggest this, though I wasn’t sure if you were on a single.php or not. I will add this to the post, it’s a question we’ve received several times.

  4. I just figured it out. It was kind of a silly/obvious question to begin with but I think it’s a relevant point for other WordPress Newbies…

    In your example you are pulling duplicate fields from a specific post ID. But to get duplicate fields from the current page, you don’t need the new WP_Query if you are already in the loop. Because my code is published on a post template, I was already in the following loop:

    <?php if ( have_posts() ) : while ( have_posts() ) : the_post(); ?>

    and therefore just had to delete the following to get it to pull only the current page:

    <?php $my_query = new WP_Query('page_id');
    while ($my_query->have_posts()) : $my_query->the_post();
    $do_not_duplicate = $post->ID; ?>

    as well as the subsequent

    <?php  endwhile;?>

    Thanks for your help again.

  5. Jesse,

    I’ve checked the links and you must have made some updates since this comment. Are you still needing assistance?

  6. Major bug: Every post is pulling every custom field from all posts, rather than that posts’ specific ID. As a result, all photos from all posts are displaying in every single post.

    <?php $my_query = new WP_Query($post->ID);
      while ($my_query->have_posts()) : $my_query->the_post();
      $do_not_duplicate = $post->ID; ?>
      
    <?php $total = getGroupDuplicates('slideshowA');?>
    <?php for($i = 1; $i < $total+1; $i++):?>
    <?php echo "<li>";
                echo "<a href='";
                if($slideshowA != '') {
                  echo get('slideshowA',$i,1). "'>";
              } else {
                echo get('slideshowLi',$i,1). "'>";
              }
                echo "<img src='" .get('slideshowLi',$i,1). "'/>";
                echo "</a>";
                echo "</li>"; ?>
    <?php endfor;?>
    <?php  endwhile;?>

    Help! Thanks so much.

  7. Jesse,

    Turn off the “Edit in Place” feature. Settings > Flutter > Other Options > Edit-n-place – uncheck this box.

  8. I’m one step closer! The fields display, but the mark-up is messed up.

    Instead of displaying a div with class “blockQuote” as specified in my php echo tag, it gives me this:

    <div class=" EIP_textbox  EIP_postid330  EIP_mid_632"></div>

    And the fields aren’t displayed in the paragraph tags that I wrapper around them in the echo tag. Thanks again for your help.

  9. lukusdukus says:

    Boom!

    Just like that it works! it sure make much more sense to call the group name rather than the first iteration of the field within that group! but fantastic it works, this will open up wordpress a massive amount for me thanks very much for sharing the knowledge!

    Cheers
    Luke

  10. lukusdukus,

    This is the same issue Jesse is having. The following should solve the problem and I will make a better note of this in the tutorial.

    <?php $total = getGroupDuplicates('team-member-name');?>
     <?php for($i = 1; $i < $total+1; $i++):?>
     <?php echo "<div id='test'>";
                echo "<h2>" .get('team-member-name',$i,1). "</h2>";
                echo get_image('team-member-image',$i,1);
                echo get('team-member-description',$i,1);
                echo "</div>"; ?>

  11. Jesse,

    I’m not sure how I keep missing this. I’m going with it doesn’t make that much sense. Change your code to this:

    <?php $total = getGroupDuplicates('block-quote');?>
     <?php for($i = 1; $i < $total+1; $i++):?>
     <?php echo "<div class='blockQuote'>";
                echo "<p>" .get('block-quote',$i,1). "</p>";
                echo "<p>" .get('by-line',$i,1). "</p>";
                echo "</div>"; ?>

    We’re changing the getGroupDuplicates to the name of the first field, not the name of the the entire group. The whole group name is a more logical choice and seems to be throwing others off as well.

  12. Yea… They’re all filled in. I can retrieve the original field (not the duplicate) using the get_post_meta tag, but nothing shows up using that code. Any other ideas? Thanks for your quick response!

  13. Jesse,

    Are the fields populated with information? This can throw you off, if not just add some quick information in there and review the output again.

  14. I’ve read all the other comments and tried their suggestions, and I can’t figure out why the code isn’t outputting anything. I’m sure of the page ID and the names of the custom group and fields.

    Here is the code from my Testimonials.php

    <div id="twoThirds">
     <?php $my_query = new WP_Query('page_id=330');
                while ($my_query->have_posts()) : $my_query->the_post();
                do_not_duplicate = $post->ID; ?>
          
                <?php $total = getGroupDuplicates('quoteGroup');?>
                <?php for($i = 1; $i < $total+1; $i++):?>
                <?php echo "<div class='blockQuote'>";
                           echo "<p>" .get('block-quote',$i,1). "</p>";
                           echo "<p>" .get('by-line',$i,1). "</p>";
                           echo "</div>"; ?>
    
                 <?php endfor;?>
     <?php  endwhile;?>        
    </div> <!-- End twoThirds -->

    The page I’m applying this to is: http://cambridgeuplighting.com/testimonials

    Thanks alot! This is going to help me out ALOT once I get it working.

  15. lukusdukus,

    I believe this is the same issue Dee was having. Because the code is being used on page.php it is not necessary to get a specific page id. Try this:

    <?php if (have_posts()) : while (have_posts()) : the_post(); ?>
         
        <?php $total = getGroupDuplicates('meet-the-team');?>
        <?php for($i = 1; $i < $total+1; $i++):?>
        <?php echo "<div id='test'>";
              echo "<h2>" .get('team-member-name',$i,1). "</h2>";
              echo get_image('team-member-image',$i,1);
              echo get('team-member-description',$i,1);
              echo "</div>"; ?>
         
        <?php endfor;?>
    <?php endwhile; endif; ?>
    

    Also, be sure that the fields are populated with information, since without any information nothing will output. Let me know how this goes.

  16. lukusdukus says:

    Doc4

    apologies code outputting 0 was </code

Leave A Comment

Please wrap any code within the <code> tag to display properly

IE6 Upgrade Option utilizes the 7.9kb script created by mihai.ile: http://code.google.com/p/ie6-upgrade-warning/ as a WordPress plugin. I preferred this script over the many alternatives...

The WordPress Plugin: WordPress Database Backup by Austin Matzko is one of the more intuitive backup plugins currently available and with no stern warnings to scare off the faint of...

While doing our usual run of site updates and code adjustments we ran into a small issue, how to display WordPress bookmarks with both text and images. Utilizing the "Links" tab ( WordPress...

BookMaster is our answer to what we feel is an odd issue with the WordPress wp_list_bookmarks tag. For those that have exercised the use of wp_list_bookmarks you are well aware that...

Limit-Post is one of the better WordPress post content limiters we have come across both in terms of usability and size. Developed by labitacora.net Limit-Post provides excellent control...