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
Sorry, I forgot to wrap the code before.
I’m closer, your tutorial helped a lot, and was able to display a single image & description field – but still not sure how to display the duplicate image fields. Can you give me a hand?
Here’s what I have so far:
Thanks so much for this post. I was ripping my hair out all day trying to understand the wiki (my Spanish is limited to asking for beer), and it was really so straight forward.
Fabrizio,
Can you send over the code again but enclosed within the “code” html tags for readability? Thank you.
Hi, great post!
I’ve a problem. I’ve a magic field CheckBox List named ‘trattamenti_corpo’ (duplicated) and a Write Pannel named ‘Servizi’ (duplicated).
When i call it in single.php with this code
Servizi Offerti
I received only the text “Array”.
Where is the error?
thx for attention!
Quick update: Duplicate image fields within a non-duplicating Group. This will output the image url.
Found a workaround had to set the value to ‘o’ and not 0, code example is below.
Great post and site!
I was wondering if you’ve had an issue with duplicated image fields within a group (the group cannot be duplicated)
Here Is my code:
instead of the url of the image being returned I just get the word “Array” for each image added.
Ron,
If you are duplicating a single field use the lower code, if duplicating a whole group use the upper code bit. It seems you want the lower code sample to display the duplicate field. Try something like the following:
Peter,
Hopefully you’ve solved this already. Try removing the quotes and periods around .#item. As shown in the third code example.
I have created some magic fields for a page(not post) and when I upload duplicated images i cant display them using the code;
i get an error on the page saying:
‘Warning: Invalid argument supplied for foreach() in C:\wamp\www\wp-content\themes\cim\sodebar_page.php on line 100′
why?
Hello. First off, great tutorial…and great comments
I’ve been struggling for a while to duplicate my field group. I have a group with a duplicating field for a photo slideshow. I’ve been using the sample code that people have been posting:
An image shows up, but only one. When I change the number in the brackets [1] to 2,3,4, those images will show up. Wondering what is going on. Do I need to create some kind of variable to increment like i++?
Any thoughts would be appreciated.
Thanks,
Ron
Andy,
The code didn’t display properly. Please be sure to use HTML code tags around your markup. Thanks
Hey Dale
This has been a great help thanks!
Just a question…I am using your code to list some album tracks using the following:
Only problem is I want to display the audio player but I do not know how to get it to work inside the query
Any ideas?
Thanks
Andy T
Answer to Andy and Sebastian.
“department” is a field in a group can’t be duplicate. “person” is a field that can be duplicate.
This outputs something like this:
WORK
- John
- Kean
- Maria
FUN
- Karl
- Joe
Hi,
I’m creating a food menu for a restaurant and am using the duplicate groups as each dish on the menu. In the group there are 6 fields (name, price…). The field that I want to duplicate is a field that will only be filled in for certain dishes e.g. burgers – toppings (which is why I want it to check if it is empty or not). If there is a choice of 3 different toppings, I would need to duplicate the field 3 times. Code for duplicate field below:
Group code without the duplicate field, the field I want to duplicate is options and if possible o-price too:
If this is going to be too difficult could you maybe help with a solution for checking if the field is empty or not because I need to do that for three of the fields. Or check the group and only output the filled in fields!? At the moment, if I check the HTML with Firebug all 6 fields are shown for every duplicated group whether they have any info in them or not.
Thank you so much for your help
Andy
Andy,
Can you post an example of what you are doing in the form of the example below. I’m curious if you are unnecessarily using the code for Duplicate Fields within Duplicates Groups. Additionally, please post the code you are currently using and be sure to wrap it in the “code” tags for this comment section.
Group Name: (Campground Name)
• Field Name: (Student Name)
Hi!
Great that there’s a solution to the duplicate fields in duplicate groups! I haven’t got it working correctly though, the field is duplicating itself in every group not just the one the information is entered in, in WordPress.
Also it is going to be empty sometimes so I Just wondering how I would make it so that if the duplicate field was empty it wouldn’t show!? I think it’s something using some like an if and true/false statements but no idea how to do it!?
Don’t know if the two points are related?
Thanks
sebastian,
This question has been asked many, many times and I’m happy to say we finally have an answer for everyone. I have been in talks with David Valdez from the Magic Fields plugin project for a few weeks now and he was kind enough to help get the ball rolling. The consistent problem was that we were unable to echo all duplicate fields within a duplicate group.
In the example below I have set up a duplicate field within a duplicate group and echoed the duplicate field within an unordered list. The code is set up this way to make it easier to add additional styling to the fields.
I will be adding this code to the tutorial soon but for now please refer to this post.
I have a duplicate group, and in this duplicate group i have a duplicate field.
The output must be
list 1
item 1.1
item 1.2
item 1.3
list 2
item 2.1
item 2.2
item 2.3
to display a duplicate group i use this code :
to display a duplicate field i use :
if my duplicate field “list” is in my duplicate group “myGroup”, what
can i display the duplicate-field ?
thx
plisvb,
The code didn’t make it through, please be sure to wrap any code in the “code” tags.
Got this to work finally for the duplicate images problem:
<?php echo '’; ?>
sereal,
Can you explain what exactly you are doing tat isn’t working?
• What code are you using?
• Where are you placing the code?
I try to follow your instructions and put it into my template but nothing showing up on the page.
please help
The forum members over at Magic Fields Support have answered Sebastien with the following which I have translated using the above post examples:
@ the last two comments: I agree! I also need to know how to have multiple duplicate groups, and duplicate fields within those groups.
Thanks!
I have an image in each group. What can i display each photo?
i used this code:
$team = get_group(‘team’);
foreach($team as $member){
echo $member['namemember'][1];
echo “”;
echo $member['photomember'][1];
echo “”;
}
for the name it’s ok, but the photo is always the same for each member…
could you help me ?
Andy,
We haven’t really explored this concept but we will look into it.
Hi,
Still racking my brains about this one!? I think I am asking an obvious question but I just need to be told either way?
How do I duplicate a field within a duplicating group? I doesn’t work if I just drop the duplicate field code inside my duplicate group code (I have changed the names)? My code is still the same as below!
Thank you
Thanks that worked great! Another question though!?
How would I go about showing duplicates of each field within a group? I have included one of my groups below:
As you can see it is for a menu and I need to be able show the duplicates of each field!? At the moment I have each group showing up fine but only showing the first entry of each field!
Thanks
Andy,
Glad we could help. if you need to display a second Group (not a duplicate of the first Group) you just need to copy the code and change out your variables (i.e. the new Group field names). Unless this second Group is from a new post or page, then you would need a new loop. Hope that makes sense.
Hi,
Great post! I wouldn’t have been able to use MF if this wasn’t around!
How would I go about getting two or more different groups displayed? I got one group displayed with the ‘Duplicate a Custom Field Group’ code but I would like to repeat this code to display another group (and more if possible)?
Would it require a whole new loop or query or could I do ‘else;’? Sorry if I am being unclear!
Would really appreciate some help?
Thanks
Thanks for all your help. I found the fix on a page I had to translate
– Hiding in the depths of the internet.
<?php echo get_image ('magicfield', 1, 1, 0); ?>This prints JUST the img source and means the user doesn’t have to input the file name manually!
Many thanks.
Tim,
Since we know where the image resides you can fill in the link for them like so:
<a href="http://www.mysite.com/wp-content/uploads/<?php echo get_post_meta($post->ID, 'img-name', $single=true) ?>"><?php echo get_post_meta($post->ID, 'img_name', $single=true) ?></a>. Now use the Text field to enter only the name of the image for example: my-image.jpgDoes this work or are you specifically wanting to use the Image field?
Many thanks for the reply. – As it’s a CMS, I would need to get the image source dynamically, as the user will be using the image upload through Magic Fields and wouldn’t know what the link would be to put in a text field. Is there a way to do this specifically?
Cheers,
Tim
Tim,
Please be sure to wrap any code within the
<code>tag to display it properly in the comments. Here is how I handle this personally. Instead of using the Image field from option try the Text field. On the post or page enter the link into the Text field for example: http://www.doc4design.com/myimage.jpg. Then in the template code<a href="<?php echo get_post_meta($post->ID, 'img-src', $single=true) ?>"><?php echo get_post_meta($post->ID, 'img_src', $single=true) ?></a>. Is this what you are looking for?Sure thing, sorry
I’m wanting to link to an image that’s inputted using magic fields, so need to print the src in
<a href="image-src" rel="nofollow">link</a>.If I used
<?php echo get_image('image'); ?>then the code will become<a href="<img src="image-src" / rel="nofollow">">link</a>which will obviously fail.Is there a way of doing something like
<?php echo get_image_SRC('image'); ?>?Many thanks
TIm,
Can you be more specific with this question? Thanks
Does anyone know how to print the src of an image alone? I need to link to it in an a href and obviously echo get_image prints the too. Help, please!
You pick one at random like this:
Anyone know how can we loop through all the duplicate groups and only display one of them at random?
Got this to work for an image…. Really comes in handy.
<?php echo $event['side-image'][1]['o']; ?>how to use this with get_post_meta?
Does anybody know how to put a duplicating field within a duplicating group? This is puzzling me!
Getting this to work with images and duplicated groups had me stumped for a little bit too…
Here’s my php snippet in full:
hope that helps someone
I was having trouble with duplicate images too, but finally got it working with this
$images = get_field_duplicate(‘images’);
foreach($images as $image){
echo ‘ ‘;
}
for anyone looking that comes across this post and is still baffled
Bogdan,
Give this a try:
echo $event['image'][1]['o'];orecho $event['image'][1]['t'];‘o’ = original image
‘t’ = thumbnail of image
Doesn’t work with images… echo $event['image'][1]; outputs “Array” instead of an image
[...] here to see the original: Tutorial: Magic Fields Duplicates | Doc4 Share and [...]
Logan,
This method should work for image fields as well. Double check your Custom Field names to be sure they are named correctly within the code, this is the most common error I see. Let me know if this corrects the problem.
How would one make this work for duplicate image fields? I’ve tried everything – and it doesn’t seem to work. Thank you!