Gravity Forms columns side by side

2019-06-03 09:08发布

In case you are wondering how to have side by side columns in Wordpress gravity forms.

Hope this saves you time.

2条回答
太酷不给撩
2楼-- · 2019-06-03 09:19

This is taken from here and here, and simplified.

First go to Form Settings and add class two-column to Form Layout.

Then go ahead and add a section break in the beginning of your Gravity Form items. Add another section break to where ever you want your form to split. Then add class gform_column to both section break's from Advanced tab.

After this go to your functions.php and paste the following (hook) :

function gform_column_splits($content, $field, $value, $lead_id, $form_id) {
if(!is_admin()) { // only perform on the front end
    if($field['type'] == 'section') {
        $form = RGFormsModel::get_form_meta($form_id, true);

        // check for the presence of multi-column form classes
        $form_class = explode(' ', $form['cssClass']);
        $form_class_matches = array_intersect($form_class, array('two-column', 'three-column'));

        // check for the presence of section break column classes
        $field_class = explode(' ', $field['cssClass']);
        $field_class_matches = array_intersect($field_class, array('gform_column'));

        // if field is a column break in a multi-column form, perform the list split
        if(!empty($form_class_matches) && !empty($field_class_matches)) { // make sure to target only multi-column forms

            // retrieve the form's field list classes for consistency
            $form = RGFormsModel::add_default_properties($form);
            $description_class = rgar($form, 'descriptionPlacement') == 'above' ? 'description_above' : 'description_below';

            // close current field's li and ul and begin a new list with the same form field list classes
            return '</li></ul><ul class="gform_fields '.$form['labelPlacement'].' '.$description_class.' '.$field['cssClass'].'"><li class="gfield gsection empty">';

        }
    }
}

return $content;
}

add_filter('gform_field_content', 'gform_column_splits', 100, 5);

This will close the ul and open a new one, which can be style to be next to each other.

Now just add the following to your styles

.gform_wrapper.two-column_wrapper ul.gform_fields {
display: none;
}
.gform_wrapper.two-column_wrapper ul.gform_fields.gform_column {
display: block;
float: left;
width: 50%;
}
.gform_wrapper.two-column_wrapper ul.gform_column li.gsection:first-child {
display: none;
}

And that should do the magic.

查看更多
甜甜的少女心
3楼-- · 2019-06-03 09:35

If you have Gravity Forms 1.5 or newer you can just use "Ready Classes".

For 2 Columns: Simply create two fields you want side by side and add the Custom CSS class gf_left_half to the first field and gf_right_half to the second one.

For 3 Columns: Use classes gf_left_third, gf_middle_third, and gf_right_third for the fields.

Inline fields: Use gf_inline for each field.

For other classes including list-item columns check out the Gravity Forms CSS Ready Classes Documentation.

查看更多
登录 后发表回答