Skip Navigation

[Resolved] I want to display only first and last of selected check boxes.

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.

No supporters are available to work today on Toolset forum. Feel free to create tickets and we will handle it as soon as we are online. Thank you for your understanding.

Sun Mon Tue Wed Thu Fri Sat
- 9:00 – 13:00 9:00 – 13:00 9:00 – 13:00 9:00 – 13:00 9:00 – 13:00 -
- 14:00 – 18:00 14:00 – 18:00 14:00 – 18:00 14:00 – 18:00 14:00 – 18:00 -

Supporter timezone: Asia/Karachi (GMT+05:00)

This topic contains 6 replies, has 3 voices.

Last updated by yosukeS 1 year, 5 months ago.

Assisted by: Waqar.

Author
Posts
#2502507
Touring Sports Damper Coilover - Revel USA 2022-11-23 at 3.38.04 PM.jpg

Tell us what you are trying to do?
I have a custom field (multi-checkboxes) representing the year for car models. On every post, we select multiple-year checkboxes to show what years the particular car was sold ex. 1998, 1999, 2000, 2001

We want to keep the data this way because you can search for particular year,
but on the like on the screen shot, we would like to display only the range ex. "1994 - 2001"

How can we set up to display like this?

Is there any documentation that you are following?
no

Is there a similar example that we can see?
no

What is the link to your site?
hidden link

#2502811

Minesh
Supporter

Languages: English (English )

Timezone: Asia/Kolkata (GMT+05:30)

Hello. Thank you for contacting the Toolset support.

You can use the index attribute to target the specific option of your checkboxes field. For example:

[types field="your-field-slug" option="0"][/types] - [types field="your-field-slug" option="9"][/types]

Considering that your last option is 10th option then the last option index will be 9.

More info - Checkboxes:
=> https://toolset.com/documentation/customizing-sites-using-php/functions/

#2503365

Thank you for providing great information.
It's good to know that I can display specific values by the shortcode.
The code you showed me display the value from a specific check box right?
If there are 2 cars. one is ranged "1993 - 2000" but 2nd car is range "1999 - 2015" How do I automate this?

I was guessing the checkboxes have orders from young number to old numbers. If I somehow set the view to show one view shortcode displays the first checked box from the items in ascending order, and the another code displays the first checked box from the items in the descending order, this might work. But I don't know how or Toolset can even do something like this.

Do you know what I mean?

#2503675

Minesh
Supporter

Languages: English (English )

Timezone: Asia/Kolkata (GMT+05:30)

The code you showed me display the value from a specific check box right?
==>
Yes.

If there are 2 cars. one is ranged "1993 - 2000" but 2nd car is range "1999 - 2015" How do I automate this?
==>
Sorry - I do not get it at all. Can you please tell me with test case example and allow me to check your current setup and also tell me what is your expected result and then I will be able to guide you in the right direction..

*** Please make a FULL BACKUP of your database and website.***
I would also eventually need to request temporary access (WP-Admin) to your site. Preferably to a test site where the problem has been replicated if possible in order to be of better help and check if some configurations might need to be changed.

I have set the next reply to private which means only you and I have access to it.

#2508753

Any updates??

#2509591

Waqar
Supporter

Languages: English (English )

Timezone: Asia/Karachi (GMT+05:00)

Hi,

Minesh is on vacation, so I'll be following up on this ticket.

To show only the first and the last selected options from a checkboxes type field, you can register a custom shortcode:


add_shortcode( 'show_year_to_from', 'show_year_to_from_func');
function show_year_to_from_func( $atts ) {
	$atts = shortcode_atts( array(
		'id' => '',
		'field' => '',
		'separator' => '',
	), $atts );

	// slug of the field
	$field_slug = $atts['field'];

	// getting the field's output separated by,
	$field_output = types_render_field( $field_slug, array( 'item' => $atts['id'], 'separator' => ',' ) );

	// if there are some values returned, explode the string into an array and return the count
	if(!empty($field_output)) {
		$field_output_arr = explode(',', $field_output);
		if (count($field_output_arr) >= 2 ) {
			return $field_output_arr[0].$atts['separator'].end($field_output_arr);
		}
		else
		{
			return $field_output_arr[0];
		}
	}
}

The above code snippet can be included through either Toolset's custom code feature ( ref: https://toolset.com/documentation/adding-custom-code/using-toolset-to-add-custom-code/ ) or through the active theme's "functions.php" file.

This custom shortcode accepts three parameters:

a). 'id' of the post to get the values from
b). 'field' slug of the target field to get the values from
c). 'separator' to join the first and the last values

So, for your field's case, it would look like this:


[show_year_to_from id="[wpv-post-id]" field="year" separator=" - "]

I hope this helps and please let me know if you need any further assistance with this.

regards,
Waqar

#2510065

My issue is resolved now. Thank you, Wagar and Minesh

This ticket is now closed. If you're a WPML client and need related help, please open a new support ticket.