Twitter
Tutorial: Magic Fields
19. May 2010 · Author: Dale Crum
67 Comments

Tutorial: Magic Fields Duplicates

Magic Fields provides a greater amount of control and ease-of-use over Flutter when it comes to duplicate fields. Unfortunately the instructions needed to use duplication are still difficult to locate, mainly because the manual hasn’t been fully translated from Spanish and posted on the Magic Fields website. It will happen soon, so hang in there. Because we have had so many questions regarding the duplication of custom Flutter fields, we wanted to demonstrate how this same feature can be achieved using Magic Fields.

Setup the Custom Write Panel:

Begin by creating a custom write panel page titled ‘Upcoming Events’. Within this newly created page, create a group titled ‘Event’ and be sure the option for duplication is checked. Finally create two custom fields for the group. Name the first field ‘even-title’ with a label of ‘Title’ and name the second field ‘event-date’ with a label of ‘Date’. Keep in mind the label will only be seen within the WordPress dashboard as a method of identifying the new custom field.

WRITE PANEL:
Name: Upcoming Events

GROUP:
Name: Event

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

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

Duplicate a Custom Field Group:

To duplicate the entire group, we only need a few lines of code. In the example below, we are using the variable ‘$myEvent’ but please use a variable that makes sense to you, keeping the dollar sign($) in front.

<?php $myEvent = get_group('Event'); // use the Custom Group name
  foreach($myEvent as $event){
  echo $event['event-title'][1]; // use the Custom Field name
  echo $event['event-date'][1]; // use the Custom Field name
  } ?>

To style either the event-title or the event-date with div tags we might do something such as:

<?php $myEvent = get_group('Event'); // use the Custom Group name
  foreach($myEvent as $event){ ?>
  <div class="meta-headline"><?php echo $event['event-title'][1]; ?></div> // use the Custom Field name
  <div class="meta-date"><?php echo $event['event-date'][1]; ?></div> // use the Custom Field name
  <?php } ?>

Duplicate a Single Custom Field:

To duplicate only one of the custom fields and not the entire group, we could use the following code.

<?php $mytitle = get_field_duplicate('event-title'); // use the Custom Field name
 foreach($mytitle as $title){
 echo  $title;
 } ?>

Again, to style our custom field output, we might want to place div tags around the title, like the following example demonstrates. Be sure to use single quotes when working within double quotes. For example in Line 3 ‘meta-headline’:

<?php $mytitle = get_field_duplicate('event-title'); // use the Custom Field name
 foreach($mytitle as $title){
 echo "<div class='meta-headline'>" . $title ."</div>";
 } ?>

A big thank you to Gnuget (David V) and Hunk (Edgar G) for keeping this plugin alive and kicking.

67 Comments

  1. VJ,

    You might try them with dashes instead and let me know what happens.

  2. Dale,

    Yes i have used underscores in variable names.. Are they creating such warnings?? As i am getting the data but its giving warnings number of times of the foreach loop..

  3. VJ,

    Are you finishing this off with echo $entry;? I’ve also run into issues with naming fields using underscores.

  4. HI,
    I am using this type of code

    $related_entry = get_field_duplicate(‘related_fields’);
    foreach($related_entry as $entry)
    {
    .. some code here…
    }

    Now my foreach loop is giving warnings on cpanel hosted server..
    Invalid argument supplied to foreach() on line

    This is due to get_field_duplicate returns an Object Array and not a nornal Array..

    Any suggestion how to pass a normal array to foreach loop here???

  5. Scott,

    Please send an example of how you are using the code. Hopefully this will clear up the issue.

  6. Hi!

    I’m having the exact same problem as ryan regarding getting the error “Warning: Invalid argument supplied for foreach()” on June 20th, 2011- in my case (and possibly his) I’m placing the code on a” Single Page” and not using it on a Post basis – does that affect anything?

  7. Courtney,

    It’s hard to say unless we can see the code you were using. But as long as you have it working that’s the important thing.

  8. Courtney says:

    Aaaaand… I also just found a solution, although I’m not sure why it works and the example you gave didn’t. I’m definitely not working with groups.

    http://wordpress.org/support/topic/plugin-magic-fields-duplicate-image-field-in-a-group

  9. Courtney says:

    Hi, thanks for this page. I copied your code into my example to see if I could get it to work… and instead of returning the field value, it returns “Array” for each item.

    My set up is a write panel with a duplicate field (not in an a group). It seems I must be missing something. Have you run into this?

    Thanks!

  10. baalstorm says:

    resolved the question below:

  11. baalstorm says:

    Hello! it worked. but for the audio field. (not that much into php) i know there’s the $event missing there, but i don’t know where to put it.
    Any help? Thank you

    
    <?php $myEvent = get_group('podcast');?>
    <?php foreach($myEvent as $event){ ?>
                <div id="archivio">
                <div id="thumb"><?php echo "<img src='".$event['podcast_immagine'][1]['t']."'>" ?></div>   
                <h3 align="center"><?php echo $event['podcast_titolo'][1];  ?></h3>  
                <div id="audio"><?php echo get_audio('podcast_audio',1); ?></div>
                <div class="entry-summary"><?php echo $event['podcast_descrizione'][1];  ?></div>
    

  12. Any ideas on how to sort groups by a field within that group? for example:

    
      $performances = getGroupOrder('performanceDate');
      foreach($performances as $performance) {
    

    I want to list each ‘performance’ group by the field ‘performanceDate’.

    Thanks!

  13. Jim,

    I haven’t gone too far into this but you might try using that “if” statement outside the duplicate code by checking for content first and if that content exists display the duplicate php code. You shouldn’t need the “else” statement at this point.

    <?php if( get_post_meta($post->ID, 'campustwo', true) ) { ?>
        My Code Here
    <?php } ?>

  14. ryan,

    The code looks correct, has this code been placed within a loop?

  15. ryan says:

    I’m having a problem with the following code:

    <?php $myPosition = get_field_duplicate('employment-position'); // use the Custom Field name
         foreach($myPosition as $position){
         echo  $position;
        } ?>

    The error that’s coming up on my page is “Warning: Invalid argument supplied for foreach()” This seems pretty straight forward but I can’t seem to figure out what the problem is. Any help is greatly appreciated.

  16. I’ve almost got Magic Fields working perfectly. I’m trying to pull in some duplicate fields but I need to run an if statement to check for a value and if nothing is present then show something else. I had it running like this without duplicate fields

        <?php $campustwo=get_post_meta($post->ID, 'campustwo', true); ?>
        <?php if ( $campustwo ) : ?>
      <tr>
        <td class="tableinfo"><?php echo $campustwo; ?></td>
        <td class="tablewhite"><?php echo get('completiontime', 2); ?></td>
        <td class="tablewhite"><?php echo get('days', 2); ?></td>
        <td class="tablewhite"><?php echo get('time', 2); ?></td>
      </tr>
        <?php else : ?>
        <?php endif; ?> 

    and it worked great but I can figure out how to run it with the duplicate code.

    This is what I have so far. I need an if statement for every TR

    
    <?php $ScheduleGroup = get_group('schedule'); // use the Custom Group name
      foreach($ScheduleGroup as $event){ ?>
    
    <?php $semester=get($event['schedule_semester'][1]); ?>
        <?php if ( $semester ) : ?>
        <h3><?php echo $semester; ?> Schedule</h3> 
    <table width="500" border="0" cellpadding="8" cellspacing="8">
      <tr >
        <td width="111" class="tabletitle">Campus</td>
        <td width="105" class="tabletitle">Completion Time</td>
        <td width="105" class="tabletitle">Days</td>
        <td width="105" class="tabletitle">Time</td>
      </tr>
    
      <tr>
        <td class="tableinfo"><?php echo $event['campusone'][1]; ?></td>
        <td class="tablewhite"><?php echo get('schedule_campus_1_completion_time', true); ?></td>
        <td class="tablewhite"><?php echo get('schedule_campus_1_days', true); ?></td>
        <td class="tablewhite"><?php echo get('schedule_campus_1_time', true); ?></td>
      </tr>  
    </table>
        <?php else : ?>
        <?php endif; ?>
    <?php } ?>
    

    Any help is much appreciated!

  17. Steve,

    I’ve tried to keep this simple but give the following a try. Remember you will need the group name to initiate the call.

    
    <?php $mySlide = get_group('YourGroupName');
      foreach($mySlide as $slide){
      echo "<a class='MagicThumb' href='";
      echo $slide['slide_detail_add_image'][1];
      echo "'/>";
      echo "<img src='";
      echo $slide['slide_detail_add_image'][1];
      echo "' width='500px'/></a>";
      echo $slide['slide_detail_description'][1];
      } ?>

Leave A Comment

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

Multicons is a multi-favicon code generator which automatically inserts the necessary meta tags for both favicons (site-wide and/or admin) and Apple Touch icons. Not sure what a...

IE6 Upgrade Option utilizes the 25K script created by Free the Foxes: http://www.freethefoxes.com/ as a WordPress plugin. Originally this plugin utilized a smaller 7K script but it's...

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 (...

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...