Tell us what you are trying to do? My CPT includes a field for weight in pounds. It currently displays as '2lb'. The client would like all weights below 3lb to display in ounces. This requires multiplying the value by 16 if the value is less than 3 and then appending 'lb' at 3 and above and 'oz' below 3.
What is the most efficient way to do this?
Is there any documentation that you are following? I couldn't find a way to add a field that would automatically calculate its value from the 'weight' field value.
I couldn't figure out how to perform the calculation within a conditional in Views.
Is there a similar example that we can see? n/a
What is the link to your site? hidden link
Admin access is still in place from prior tickets.
Hi,
Thank you for contacting us and I'd be happy to assist.
To achieve this, you can register a custom shortcode, that gets the value from the weight custom field and then return the output, after the necessary calculation and suffix.
For example:
add_shortcode('custom_show_weight_field', 'custom_show_weight_field_func');
function custom_show_weight_field_func() {
$weight_field_slug = 'recipe-weight';
$field_value = types_render_field($weight_field_slug, array("output" => "raw"));
if($field_value >= 3) {
$final_value = $field_value.'lb';
}
else
{
$final_value = ($field_value*16).'oz';
}
return $final_value;
}
This shortcode uses a slug 'recipe-weight' for the weight custom field, but you can change it as per your website.
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.
Next, where you'd like to show this field's output, you can use the shortcode like this:
[custom_show_weight_field]
I hope this helps and please let me know if you need any further assistance with this.
regards,
Waqar