Problem: I would like to create a select field with options based on a post's custom field values, and use that field in Contact Form 7.
Solution: Use get_post_meta to get all the values of the custom field for the current post, then loop over those values to build the markup for a select field.
Get the colors postmeta values for this post as an array, then loop over the array items to concatenate the options. [php] global $post; $output = '<select name="color">'; $colors = get_post_meta($post->ID, 'wpcf-color', true); // 3rd param 'true' because we want an array foreach( $colors as $color) { $output .= '<option value="' . $color . '">' . $color . ' </option>'; } $output .= '</select>'; return $output;
Relevant Documentation:
https://codex.wordpress.org/Function_Reference/get_post_meta
http://php.net/manual/en/language.operators.string.php
This is the technical support forum for Toolset - a suite of plugins for developing WordPress sites without writing PHP.
Everyone can read this forum, but only Toolset clients can post in it. Toolset support works 6 days per week, 19 hours per day.
Sun | Mon | Tue | Wed | Thu | Fri | Sat |
---|---|---|---|---|---|---|
8:00 – 12:00 | 8:00 – 12:00 | 8:00 – 12:00 | 8:00 – 12:00 | 8:00 – 12:00 | - | - |
13:00 – 17:00 | 13:00 – 17:00 | 13:00 – 17:00 | 13:00 – 17:00 | 13:00 – 17:00 | - | - |
Supporter timezone: America/New_York (GMT-04:00)
This topic contains 3 replies, has 2 voices.
Last updated by 6 years, 3 months ago.
Assisted by: Christian Cox.