Translations of this page:

Working with groups, duplicate groups and duplicate fields.

Magic Fields lets you create field groups (Custom Fields) this way you can separate fields (Custom Fields) and handle the fields in a better way. Groups can be duplicated.

v2_groups_and_field.jpg

Fields that don't belong to a group are placed in a “Magic Fields” box.

 


 

Groups

To create a group go to the page where we create field click on the + Create a Group button.

Once in the Create Custom Group page we have these fields: Name (this is the name of the group that will be used for the functions in the Front-end), Label (this label will be shown in the box where the fields are shown) and the duplicate group (we will go into detail of this option later on).

Once the group is created it will take us back to the add fields page, but we'll see our newly created group. If we need to edit the group options click on the group's name.

To add a field to the group click in the “create field” link just beside the group's name ot in the “create one here” link. Para agregar un campo al grupo hacemos click en el enlace “create field” delante del nombre del grupo o en link “create one here”.

The field creation in a group is the same as when we create a field without a group. Notice that when writing the field's label and moving to the next field it makes a suggestion for the field's name with the following format {group name}_{field label}.

In the Post Type field page we will see the groups created and the fields belonging to each group.

When we create an element in our Post Type we'll see it this way:

The way of showing the field's information in the template is the same as it is for a field that doesn't belong to a group, using functions like get, get_image or get_audio. To see all the functions available read our Front-end Functions.

 


 

Duplicate Groups

Magic Fields has the option to duplicate a group (duplicates all the fields in the group). We can observe the utility of this feature with the next example: We need a Post Type where we have all the members from a company.

We create a Post Type called “Team”, in this Post Type we create a group called “members” and in the group options we chose that the option so that the group may be duplicated.

Once the group is created we add these fields:

  • A textbox field with the name: members_name label: Name
  • A textbox field with the name: members_position label: Position
  • A field image with the name: members_photo label: Photo (aditional options Max Height:150, Max Width:100 and custom zc=1)

Our fields will look like this:

Now we create the Post Type elements. We'll see our group's box, we fill in the fields with the data of our first member. To duplicate the group click on the ”+ Add Another” link located on the lower right side (we can duplicate the group as many times as needed).

We will see the duplicated group with all it's fields. Now we can fill in the fields for the new member. We can notice that the “Remove” link appears for each group, with this link we can erase the group. video

One of the best features in duplicate groups is that we can arrange the groups that we duplicated. This is done by clicking and dragging the 4 point image in the lower left side of each group. Watch the video. With this feature and with the use of Front-end functions we can show the duplicate groups in order.

There are two ways of showing the groups in the template. Add the following code in your template:

    <?php
       // Duplicate Groups.
 
       // get_order_group returns an array with the group's order
       // the parameter for this function is the name of a field belonging to the group
       $members = get_order_group('members_name');
       foreach($members as $member){
          // the second parameter for the get and get_image functions is the group index to be shown
          echo get('members_name',$member)."<br />";
          echo get('members_position',$member)."<br />";
          echo get_image('members_photo',$member)."<br /><br />";
       }
    ?>

The second way of using the get_group function.

<?php
      // second way to get the fields
 
      // get_group will retunr an array of all the groups and the group's fields
      // the parameter for this function is the group's name
      $members = get_group('members');
      // to see how the array is formed you can use pr($members);
      // the way the array is formed is
      // [group index][field name][field index]
      // The image fields in the array have one more level where the letter "original" and "thumb" which will show
      // the original or the thumbnail image
      foreach($members as $member){
        echo $member['members_name'][1]."<br />";
        echo $member['members_position'][1]."<br />";
        echo "<img src='".$member['members_photo'][1]['thumb']."'><br /><br />";
      }
    ?>

In both cases the result is follows:

We can change the order of the duplicate groups and we'll notice that this new arrangement is respect in our site (without having to change the code) ver video

For more information about these parameters and the get and get_image functions read the Front-end Functions section.

The group can be duplicated as many times as needed, but we must at least have 1 group, any group can be deleted and they will be re-arranged. When there is only 1 group left the delete link will dissapear, if you add a group it will be visible again.

 


 

Duplicated Fields

Duplicated fields work in a similar way as duplicated groups.

We'll show the behavior of the duplicate fields with another example, inside a Post Type add a textbox field (name:song, label:Song) it's important specifying that the field can have a duplicate (Can be duplicated option).

After adding the duplicate field we click on new in the Post Type menu. We'll notice that under our field there is a ”+ Add another {field's label}” link, it is used to duplicate the field.

We fill in the field and add a duplicate field and click on ”+ Add another Song”, we can duplicate the field as often as necessary.

When we duplicate a field a link with the ”- Remove Song” is shown, this link lets us delete the field. In front of the field's name a number appears, this number indicates the number of duplicate field (every time we add or erase a field it will take a new order).

Now we'll see the two ways of showing the required duplicate fields in our template.

Using get_order_field and get

<?php
      // first way, using get_order_field and get
 
      // the get_order_field function returns an array with the field's index
      // the parameter for this function is the field's name
      $songs = get_order_field('song');
 
      foreach($songs as $song){
         // The first parameter for get is the field's name, the second is the group's index. If this
         // field is not in a group that can be duplicated it should be 1. The third parameter is the field's index
         echo get('song',1,$song)."<br />";
      }
    ?>

The second way is to do it inside a get_field function.

<?php
      // second way, using get_field
 
      // The get_field function returns and array with all the duplicated field elements.
      // This function has as a first parameter the field name and the second the group's index(which is optional
      // if the group is duplicated).
 
      $songs = get_field('song');
 
      foreach($songs as $song){
         echo $song."<br />";
      }
    ?>

Both ways give the same result:

Just like duplicate groups, duplicate fields can be duplicated as many times as needed, but there must be at least 1. When there is only 1 the link to remove the group is not visible, if we add a field the remove field will be available. We can erase any of the fields Video.

To know more about the function's parameters for get, get_field and get_order_field read our Front-end Functions section.

 


 

Notes

After seeing how groups, duplicate groups and duplicate fields work we can create any combination needed. If we have a write panel with fields and without a group we can create all the groups needed (either duplicate or not), we can even create a duplicate field inside a group or by itself. We can have a duplicate field in a duplicate group and show information through the Front-end function's parameters. To know more about these functions go to the Front-end Functions.

working_with_groups_duplicated_groups_and_duplicated_fields_v20.txt · Last modified: 2011/08/02 19:01 by bcorona