Skip Navigation

[Resolved] print field options in content template

This support ticket is created 6 years, 11 months ago. There's a good chance that you are reading advice that it now obsolete.

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 8 replies, has 2 voices.

Last updated by linaS 6 years, 11 months ago.

Assisted by: Christian Cox.

Author
Posts
#597428

Let's say I've got 'select' type field with options: 1, 2, 3.

I want to write it at content template like this (say 1 is selected value):
<tr>
<td>Field name:</td>
<td>[x] 1, [ ] 2, [ ] 3</td>
</tr>

I need it to send by mail for manual submission (or to print).

How would I do this?

#597499

One way to handle this is to write out all the options manually, then use Conditional HTML to show an "X" inside the selected option.

[ [wpv-conditional if="( $(wpcf-field) eq '1' )"]X[/wpv-conditional] ] 1
[ [wpv-conditional if="( $(wpcf-field) eq '2' )"]X[/wpv-conditional] ] 2
[ [wpv-conditional if="( $(wpcf-field) eq '3' )"]X[/wpv-conditional] ] 3

Replace "wpcf-field" with the slug of your custom field (prefixed by wpcf-), and replace '1', '2', and '3' in the conditional with the actual values of each option.

With a bit of custom code, you could probably automate this so you don't have to write out each option in a conditional. More information about conditional HTML can be found here:
https://toolset.com/documentation/user-guides/conditional-html-output-in-views/

#597549

Is there no API that will get me a list of options of the field? You get them internally obviously...

#597784

A custom SQL query could find all the unique values for this postmeta key, but unfortunately there is no public API available to get all the available options. Our documented APIs can be found here:
https://toolset.com/documentation/programmer-reference/

#597935

$fields = wpcf_admin_fields_get_active_fields_by_post_type('applications');

took me half an hour to find...

#597963
add_shortcode('print_types_options', function( $attr ) {
	$field = $attr['field'];
	$values = get_post_meta( get_the_ID(), 'wpcf-' . $attr['field'] );
	$result = '';
	$fields = wpcf_admin_fields_get_active_fields_by_post_type('applications');

	$fielddef = $fields[$field];
	
	if( $fielddef )
	{
		foreach( $fielddef['data']['options'] as $option )
		{
			$result .= '[' . (in_array( $option['value'], $values ) ? 'x' : '&nbsp;') . ']&nbsp;' . $option['title'] . ', ';
		}
	}		
	
	return $result;
});
#597964

that's the solution

#597969
add_shortcode('print_types_options', function( $attr ) {
	$field = $attr['field'];
	$values = get_post_meta( get_the_ID(), 'wpcf-' . $attr['field'], true );
	$result = '';
	$fields = wpcf_admin_fields_get_active_fields_by_post_type('applications');

	$fielddef = $fields[$field];
	
	if( $fielddef )
	{
		foreach( $fielddef['data']['options'] as $key => $option )
		{
			if( $key !== 'default' )
				$result .= '[' . ((is_array( $values ) && in_array( $key, array_keys($values) ) || !is_array( $values ) && $option['value'] == $values ) ? 'x' : '&nbsp;') . ']&nbsp;' . $option['title'] . ', ';
		}
		
		#$result .= '(' . print_r($values, true) . ')';
	}		
	
	return $result;
});
#597970

This is with multiple values support