Hi Dawson,
I think all your needs can be put into one shortcode,
Please try like this:
1) modify the codes in your theme/functions.php, 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)
{
$class = "";
if($v<0)$class = "my-red";
return $res = '<div class="' . $class . '">' . number_format($v, 2) . '</div>';
}
add_filter('compare_format_filter', 'compare_format_filter_func');
function compare_format_filter_func($v)
{
$res = '';
if($v<18)$res= 'A';
if($v>18 && $v<22)$res= 'B';
if($v>22)$res= 'C';
//here you can add more conditions
return $res;
}
add_filter('compare_format_filter_2', 'compare_format_filter_func_2');
function compare_format_filter_func_2($v)
{
$res = '';
if($v<5)$res= 'A';
if($v>5 && $v<10)$res= 'B';
if($v>10)$res= 'C';
//here you can add more conditions
return $res;
}
Add CSS code in your theme/style.css:
div.my-red{
color: red;
}
2) in different case, you use the shortcode with different attribute:
a) for Need #1:
put the shortcode into your content like this:
[wpv-calculate evaluate=" [types field="detached-sales-current" output="raw" id="$market-updates"][/types] / [types field="detached-active-listings-current" output="raw" id="$market-updates"][/types] * 100 "]%
The structure is:
[wpv-calculate evaluate='A / B']
b) for Need #2
put the shortcode into your content like this:
[wpv-calculate evaluate='([types field="detached-sales-current"][/types] – [types field="detached-active-listings-current"][/types])/ [types field="detached-active-listings-current"][/types]']
The structure is:
[wpv-calculate evaluate='(A – B) / B']
c) use different attribute "format_filter" for different case
Data set 1:
put the shortcode into your content like this structure:
[wpv-calculate evaluate='X' format_filter="compare_format_filter]
Data set 2:
[wpv-calculate evaluate='X' format_filter="compare_format_filter_2]
You can try create a post with codes in it and test:
[wpv-calculate evaluate='6/101*100']
--------------------------------------
[wpv-calculate evaluate='(1-14) / 102*100']
--------------------------------------
[wpv-calculate evaluate='15' format_filter="compare_format_filter]
--------------------------------------
[wpv-calculate evaluate='23' format_filter="compare_format_filter_2]