Hi there
I need to be able to take the output of a calculation and then use it to output a couple of words, depending on what the calculated total is - e.g., if calculated total is:
- Less than 18% then I need the output to show as A
- Between 18% and 22% then I need the output to show as B
- Over 22% then I need the output to show as C
This is the calculation that I need to use to to compare against the above figures.
[wpv-calculate evaluate="[types field="detached-sales-current" id="$market-updates"][/types] / [types field="detached-active-listings-current" id="$market-updates"][/types] * 100 "]%
I'm guessing that this can be done using wpv-if but it's doing my head in trying to figure out how.
Any guidance would be greatly appreciated.
Many thanks
Hi dbarber,
I assume you are using such a custom shortcode in your theme/functions.php:
https://toolset.com/forums/topic/need-to-limit-the-number-of-digits-output-from-a-calculation/#post-173568
add_shortcode('wpv-calculate', 'calculate_shortcode');
function calculate_shortcode($atts) {
return number_format(wpv_condition($atts), 2);
}
Please try modify it like this:
add_shortcode('wpv-calculate', 'calculate_shortcode');
function calculate_shortcode($atts) {
extract( shortcode_atts( array(
'evaluate' => '',
'format_filter' => 'number_format_filter'
), $atts ) );
$res = wpv_condition($atts);
$res = apply_filters($format_filter, $res);
return $res;
}
add_filter('number_format_filter', 'number_format_filter_func');
function number_format_filter_func($v)
{
return $res = number_format($v, 2);
}
add_filter('compare_format_filter', 'compare_format_filter_func');
function compare_format_filter_func($v)
{
$res = 'C';
if($v<18)$res= 'A';
if($v>18 && $v<22)$res= 'B';
//here you can add more conditions
return $res;
}
Modify the shortcode in your content like this:
[wpv-calculate evaluate="[types field="detached-sales-current" id="$market-updates"][/types] / [types field="detached-active-listings-current" id="$market-updates"][/types] * 100 " format_filter="compare_format_filter"]
Add attribute format_filter="compare_format_filter" in the shortcode
Hi Luoy
Thank you very much for this!
Tested and working perfectly.
Cheers
Dawson