我当前正在使用GravityWP Count plugin 用于数字字段上的输入总计。为此,我还需要通过一个短代码减去总数。我联系了插件作者,他友好地回应了我,但我确信还有其他事情要做。
if (class_exists("GFForms")) {
function gravitywp_count_func( $atts, $content = null ) {
extract( shortcode_atts( array(
\'formid\' => \'0\',
\'formstatus\' => \'active\',
\'number_field\' => false,
\'filter_mode\' => \'all\',
\'add_number\' => \'0\',
\'sub_number\' => \'0\',
\'filter_field\' => false,
\'filter_operator\' => \'is\',
\'filter_value\' => false,
\'filter_field2\' => false,
\'filter_operator2\' => \'is\',
\'filter_value2\' => false,
\'filter_field3\' => false,
\'filter_operator3\' => \'is\',
\'filter_value3\' => false,
\'filter_field4\' => false,
\'filter_operator4\' => \'is\',
\'filter_value4\' => false,
\'filter_field5\' => false,
\'filter_operator5\' => \'is\',
\'filter_value5\' => false,
\'decimals\' => \'2\',
\'dec_point\' => \'.\',
\'thousands_sep\' => \',\',
\'created_by\' => \'\',
\'page_size\' => \'500\',
\'is_starred\' => \'\',
\'is_read\' => \'\',
\'multiply\' => \'1\',
\'start_date\' => false,
\'end_date\' => false
), $atts ) );
if ( $formstatus != \'all\' ) {
$search_criteria[\'status\'] = $formstatus;
}
$search_criteria[\'field_filters\'][\'mode\'] = $filter_mode;
if ( !empty( $filter_field ) ) {
$search_criteria[\'field_filters\'][] = array(\'key\' => $filter_field, \'operator\' => $filter_operator, \'value\' => $filter_value);
}
if ( !empty( $filter_field2 ) ) {
$search_criteria[\'field_filters\'][] = array(\'key\' => $filter_field2, \'operator\' => $filter_operator2, \'value\' => $filter_value2);
}
if ( !empty( $filter_field3 ) ) {
$search_criteria[\'field_filters\'][] = array(\'key\' => $filter_field3, \'operator\' => $filter_operator3, \'value\' => $filter_value3);
}
if ( !empty( $filter_field4 ) ) {
$search_criteria[\'field_filters\'][] = array(\'key\' => $filter_field4, \'operator\' => $filter_operator4, \'value\' => $filter_value4);
}
if ( !empty( $filter_field5 ) ) {
$search_criteria[\'field_filters\'][] = array(\'key\' => $filter_field5, \'operator\' => $filter_operator5, \'value\' => $filter_value5);
}
if ( !empty( $created_by ) ) {
if ($created_by == "current") {
$user_ID = get_current_user_id();
$search_criteria[\'field_filters\'][] = array(\'key\' => \'created_by\', \'value\' => $user_ID);
} else {
$search_criteria[\'field_filters\'][] = array(\'key\' => \'created_by\', \'value\' => $created_by);
}
}
if ( $is_starred == "yes" ) {
$search_criteria[\'field_filters\'][] = array(\'key\' => \'is_starred\', \'value\' => true);
}
if ( $is_starred == "no" ) {
$search_criteria[\'field_filters\'][] = array(\'key\' => \'is_starred\', \'value\' => false);
}
if ( $is_read == "yes" ) {
$search_criteria[\'field_filters\'][] = array(\'key\' => \'is_read\', \'value\' => true);
}
if ( $is_read == "no" ) {
$search_criteria[\'field_filters\'][] = array(\'key\' => \'is_read\', \'value\' => false);
}
if ( !empty( $start_date ) ) {
$date_start = date_create( $start_date );
if ($date_start) {
$search_criteria[\'start_date\'] = $date_start->format( \'Y-m-d H:i:s\' ); }
else {
echo "Oops... your start_date format is not correct: " . $start_date . "
(must be Month/Day/Year)<br>Change the start_date in your gravitywp_count
shortcode.<br>";
}
}
if ( !empty( $end_date ) ) {
$date_end = date_create( $end_date );
if ($date_end) {
$search_criteria[\'end_date\'] = $date_end->format( \'Y-m-d H:i:s\' ); } else {
echo "Oops... your send_date format is not correct: " . $end_date . "
(must be Month/Day/Year)<br>Change the start_date in your gravitywp_count
shortcode.<br>";
}
}
$sorting = null;
$paging = array( \'offset\' => 0, \'page_size\' => $page_size );
$entries = GFAPI::get_entries($formid, $search_criteria, $sorting, $paging);
$countentries = GFAPI::count_entries( $formid, $search_criteria );
$countentries_add = $add_number + $countentries;
if ( !empty( $number_field ) ) {
$gwp_count = $add_number;
for ($row = 0; $row < $countentries ; $row++) {
$gwp_count += $entries[$row][$number_field];}
return number_format($gwp_count * $multiply, $decimals,
$dec_point, $thousands_sep);
} else {
return number_format($countentries_add * $multiply, "0", ".",
$thousands_sep);
}
$sorting = null;
$paging = array( \'offset\' => 0, \'page_size\' => $page_size );
$entries = GFAPI::get_entries($formid, $search_criteria, $sorting, $paging);
$countentries = GFAPI::count_entries( $formid, $search_criteria );
$countentries_sub = $sub_number - $countentries;
if ( !empty( $number_field ) ) {
$gwp_count = $sub_number;
for ($row = 0; $row < $countentries ; $row--) {
$gwp_count -= $entries[$row][$number_field];}
return number_format($gwp_count * $multiply, $decimals,
$dec_point, $thousands_sep);
} else {
return number_format($countentries_sub * $multiply, "0", ".",
$thousands_sep);
}
}
add_shortcode( \'gravitywp_count\', \'gravitywp_count_func\' );
}
Shortcode:
Budget Total: $[gravitywp_count formid="8" sub_number="250000"
number_field="37" filter_field="29" filter_value="Budget" thousands_sep=","]
但它不返回值。
add_number shortcode:
Budget Total: $[gravitywp_count formid="8" add_number="-250000"
number_field="37" filter_field="29" filter_value="Budget" thousands_sep=","]
我做错了什么?
Note: 该插件允许您使用add\\u number短代码,在减法数前面加一个-(减号),但返回一个负数。上面的短代码返回的是-245000,而不是245000。
最合适的回答,由SO网友:inarilo 整理而成
使用$add_number
如果$sub_number
是0
, 其他用途$sub_number
:
$sorting = null;
$paging = array( \'offset\' => 0, \'page_size\' => $page_size );
$entries = GFAPI::get_entries($formid, $search_criteria, $sorting, $paging);
$countentries = GFAPI::count_entries( $formid, $search_criteria );
if(!$sub_number) {
$countentries_add = $add_number + $countentries;
if ( !empty( $number_field ) ) {
$gwp_count = $add_number;
for ($row = 0; $row < $countentries ; $row++) {
$gwp_count += $entries[$row][$number_field];}
return number_format($gwp_count * $multiply, $decimals,
$dec_point, $thousands_sep);
} else {
return number_format($countentries_add * $multiply, "0", ".",
$thousands_sep);
}
} else {
$countentries_sub = $sub_number - $countentries;
if ( !empty( $number_field ) ) {
$gwp_count = $sub_number;
for ($row = 0; $row < $countentries ; $row++) {
$gwp_count -= $entries[$row][$number_field];}
return number_format($gwp_count * $multiply, $decimals,
$dec_point, $thousands_sep);
} else {
return number_format($countentries_sub * $multiply, "0", ".",
$thousands_sep);
}
}
循环仍会增加行数,不能从0开始向下计数行数。
或者如果你知道$add_number
只有在要进行减法运算时才为负数,可以使用:
$sorting = null;
$paging = array( \'offset\' => 0, \'page_size\' => $page_size );
$entries = GFAPI::get_entries($formid, $search_criteria, $sorting, $paging);
$countentries = GFAPI::count_entries( $formid, $search_criteria );
$countentries_add = $add_number + $countentries;
if ( !empty( $number_field ) ) {
$gwp_count = $add_number;
for ($row = 0; $row < $countentries ; $row++) {
$gwp_count += $entries[$row][$number_field];}
if($add_number < 0) $gwp_count *= -1;
return number_format($gwp_count * $multiply, $decimals,
$dec_point, $thousands_sep);
} else {
if($add_number < 0) $countentries_add *= -1;
return number_format($countentries_add * $multiply, "0", ".",
$thousands_sep);
}