php 用于计算WordPress的短代码

tquggr8v  于 2022-11-21  发布在  PHP
关注(0)|答案(1)|浏览(86)

我需要一个可以计算WordPress的值的短代码。
示例:

[px-calc before=[wpbb post:custom_field key=nummer_1] operation='/' after=[wpbb post:custom_field key=nummer_2] percentage='true'] // 50% using shortcode in shortcode 

[px-calc before=10 operation='/' after=20 percentage='true'] // 50%

[px-calc before=10 operation='*' after=10 calc_suffix='+ 50'] // 150
iyzzxitl

iyzzxitl1#

这个简码可以计算不同的东西。

/* CALCULATE
--------------------------------------------------- */
function px_calculate($atts) {

// Set shortcode options
$atts = shortcode_atts(
array(
'before' => '1',
'operation' => '+',
'after' => '1',
'digits' => '0',
'lang' => 'nl_NL',
'percentage' => false,
'calc_suffix' => '0',
),
$atts
);

if ($atts['operation'] == '+') {
if ($atts['percentage'] == true) {
$sum = (($atts['before'] + $atts['after']) * 100) + $atts['calc_suffix'];
} else {
$sum = $atts['before'] + $atts['after'] + $atts['calc_suffix'];
}}

if ($atts['operation'] == '-') {
if ($atts['percentage'] == true) {
$sum = (($atts['before'] - $atts['after']) * 100) + $atts['calc_suffix'];
} else {
$sum = $atts['before'] - $atts['after'] + $atts['calc_suffix'];
}}

if ($atts['operation'] == '/') {
if ($atts['percentage'] == true) {
$sum = (($atts['before'] / $atts['after']) * 100) + $atts['calc_suffix'];
} else {
$sum = $atts['before'] / $atts['after'] + $atts['calc_suffix'];
}}

if ($atts['operation'] == '*') {
if ($atts['percentage'] == true) {
$sum = (($atts['before'] * $atts['after']) * 100)  + $atts['calc_suffix'];
} else {
$sum = $atts['before'] * $atts['after'] + $atts['calc_suffix'];
}}

if (function_exists('numfmt_create')) {
$formatter = new NumberFormatter($atts['lang'], NumberFormatter::DECIMAL);
$formatter->setAttribute(NumberFormatter::MIN_FRACTION_DIGITS, $atts['digits']);
$formatter->setAttribute(NumberFormatter::MAX_FRACTION_DIGITS, $atts['digits']);
return $formatter->format($sum);
} else {
return '<strong style="color:red;">Error: Turn on php-intl on your server.</strong>';
}

}
add_shortcode('px-calc', 'px_calculate');

示例:

[px-calc before=[wpbb post:custom_field key=nummer_1] operation='/' after=[wpbb post:custom_field key=nummer_2] percentage='true'] // 50% using shortcode in shortcode 

[px-calc before=10 operation='/' after=20 percentage='true'] // 50%

[px-calc before=10 operation='*' after=10 calc_suffix='+ 50'] // 150

相关问题