Articles
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
Trackbacks
- polyfade on "[Plugin: Magic Fields] How to display all groups" | Upgrade Wordpress Now
- MAGIC FIELDS – THE EASIEST WAY TO CREATE A CLIENT READY BACK-END ON YOUR WORDPRESS SITEMy Wordpress Developer | My Wordpress Developer
- The 7 Most Important Wordpress Plugins for Clients | My Wordpress Developer
- wp-popular.com » Blog Archive » Tutorial: Magic Fields Duplicates | Doc4
- Tutorial: Magic Fields Duplicates | Doc4 | Yoobz.com
- Tutorial: Magic Fields Duplicates | Doc4 | Design Blog
- Tweets that mention Tutorial: Magic Fields Duplicates | Doc4 -- Topsy.com
VJ,
You might try them with dashes instead and let me know what happens.
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..
VJ,
Are you finishing this off with
echo $entry;? I’ve also run into issues with naming fields using underscores.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???
Scott,
Please send an example of how you are using the code. Hopefully this will clear up the issue.
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?
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.
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
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!
resolved the question below:
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
Any ideas on how to sort groups by a field within that group? for example:
I want to list each ‘performance’ group by the field ‘performanceDate’.
Thanks!
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.
ryan,
The code looks correct, has this code been placed within a loop?
I’m having a problem with the following code:
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.
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
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
Any help is much appreciated!
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.