仅当值大于0时显示ACF字段

时间:2019-02-08 作者:James

我有一个ACF领域paid_attendance - 我只希望它在值大于0时显示字段。以下代码仅在我交换>0 获取实际数字(该数字与后端为特定帖子设置的数字匹配)。

还有,如果我用<h4> 关于“付费出勤”——我如何确保<h4> 仅当值也大于0时显示?换句话说,如果我让它工作,那么它只显示paid_attendance 如果值大于0,我不希望某些帖子仍然显示“付费出勤”,旁边什么都没有(这些帖子paid_attendance = 0)。提前谢谢。

<?php
// Different versions of a number
$unformatted = get_field(\'paid_attendance\');
{
    $integerValue = str_replace(",", "", $unformatted);  // Remove commas from string
    $integerValue = intval($integerValue);               // Convert from string to integer
    $integerValue = number_format($integerValue);        // Apply number format
    $floatValue = str_replace(",", "", $unformatted);    // Remove commas from string
    $floatValue = floatval($floatValue);                 // Convert from string to float
    $floatValue = number_format($floatValue, 2);         // Apply number format
if ( \'0\' == $unformatted ){
    //do nothing

} elseif ( \'>0\' == $unformatted ) {
    echo $integerValue; 
}} 
?>

Update:

已更改( \'>0\' == $unformatted )( $unformatted >0 ) 现在它开始工作了。但是,如果有人对我上面的说明有任何了解,只显示h4 如果值也大于0,则显示文本。谢谢

Update:这就是诀窍:

elseif ( $unformatted >0 ) { 
echo \'<h4 class="paid_tix">Paid Tickets: \'; echo \'</h4>\';echo \' \'; echo \'<h4 class="integer">\'; echo $integerValue; echo \'</h4>\';

2 个回复
SO网友:phatskat

I see you\'ve found your own answer, but I wanted to offer some advice for making your code a little more succinct.

<?php
// Different versions of a number
$unformatted  = get_field(\'paid_attendance\');
$cleaned      = str_replace(\',\', \'\', $unformatted);  // Remove commas from string
$integerValue = absint( $cleaned );                 // Convert to absolute integer.
$floatValue   = number_format($cleaned, 2); // Apply number format.
// Note that number_format returns a string, so if you want
// an actual float, call floatval _now_:
$floatValue = floatval( $floatValue );

if ( 0 < $integerValue ) {
    echo $integerValue;
}

So, a few things to unpack here:

  • You can use the "cleaned" (comma-removed) string for getting both your integer and float
  • absint is a WordPress function that will convert a value to an absolute integer.
  • number_format returns a string, so calling it after floatval or intval was superfluous.
  • Your initial if statement didn\'t do anything, so you can omit it.
  • You\'re better off checking the $integerValue as it\'s an int instead of $unformatted or $cleaned since you\'re doing a numeric comparison.
    • Suppose $unformatted were the string \'a\'. In PHP, zero is not less than \'a\':
php > var_dump( 0 < \'a\' );
bool(false)
SO网友:Vasim Shaikh

$integerValue = str_replace(",", "", $unformatted);  // Remove commas from string
$integerValue = intval($integerValue);               // Convert from string to integer
$integerValue = number_format($integerValue);        // Apply number format

$floatValue = str_replace(",", "", $unformatted);    // Remove commas from string
$floatValue = floatval($floatValue);                 // Convert from string to float
$floatValue = number_format($floatValue, 2);

//Consider that  $unformatted value is converted to integer or float

if ( $integerValue > 0 ||  $floatValue > 0.00){
echo \'<h4 class="paid_tix">Paid Tickets: </h4> <h4 class="integer">\'.$integerValue/\'</h4>\';
}
说明:

您必须检查数值,以获得正确的布尔值输出,而不是$未格式化

相关推荐