Skip Navigation

[Resolved] Calculating in a number field

This support ticket is created 3 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
- 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 2 replies, has 2 voices.

Last updated by ericS-17 3 years, 11 months ago.

Assisted by: Waqar.

Author
Posts
#1605899

Tell us what you are trying to do?
I have a simple number field storing kilometers. On my content template, I want to output a nicely formatted string which includes the number in km, but also the conversion in miles. So saved value would, be, say 100. Output would be "100 km (62.1 miles)

Is there any documentation that you are following?
Forum threads, but they all reference registering a 3rd party shortcode under Views>Compatibility... and I don't see that option.

Is there a similar example that we can see?

What is the link to your site?
Here's a page showing the numbers under "Route Basics": hidden link

#1606639

Waqar
Supporter

Languages: English (English )

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

Hi Eric,

Welcome to Toolset support and I'd be happy to assist.

For conversion like this, you can register a custom shortcode.
( ref: https://codex.wordpress.org/Shortcode_API )

Using either Toolset's custom code feature ( ref: https://toolset.com/documentation/adding-custom-code/using-toolset-to-add-custom-code/ ) or through active theme's "functions.php" file, you can include the following code snippet in your website:


add_shortcode('number-km-to-mile', 'number_km_to_mile_func');
function number_km_to_mile_func( $atts ) {
	$a = shortcode_atts( array(
		'number' => ''
	), $atts );

	if (!empty($a['number'])) {
		return $a['number'] * 0.621371;
	}
}

After that, you'll be able to use this shortcode in your content this:


[number-km-to-mile number="100"]

The above shortcode will return "62.1371", which is the equivalent of 100 km in miles.

And suppose that you have a numeric custom field with slug "mileage" that stores values in km.

You can convert it through this new shortcode like this:


[number-km-to-mile number="[types field='mileage' output='raw'][/types]"]

Note: Please replace "mileage" with the actual slug of your numeric field.

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

regards,
Waqar

#1609235

Thanks for spelling it out precisely. Worked perfectly, and now I'm customizing it to meet my exact needs.

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