I have an ACF field of paid_attendance
- I only want it to display the field if the value is greater than 0. The below code only works if I swap >0
out for an actual number (and that number matches the number set on the backend for a particular post(s).
Also, if I were to wrap the below in a div with an <h4>
of "Paid Attendance" - how can I also make sure that the <h4>
only shows up if the value is greater than 0 as well? In other words, if I get this working so it only displays paid_attendance
if value is greater than 0, I dont want to have "Paid Attendance" still showing up for certain posts with nothing next to it (the posts where paid_attendance
= 0). Thanks in advance.
<?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:
Changed ( '>0' == $unformatted )
to ( $unformatted >0 )
and now its working. However, would appreciate if anyone has insight on my note above regarding only displaying the h4
text if the value is >0 as well. thanks
Update: This does the trick:
elseif ( $unformatted >0 ) {
echo '<h4 class="paid_tix">Paid Tickets: '; echo '</h4>';echo ' '; echo '<h4 class="integer">'; echo $integerValue; echo '</h4>';
I have an ACF field of paid_attendance
- I only want it to display the field if the value is greater than 0. The below code only works if I swap >0
out for an actual number (and that number matches the number set on the backend for a particular post(s).
Also, if I were to wrap the below in a div with an <h4>
of "Paid Attendance" - how can I also make sure that the <h4>
only shows up if the value is greater than 0 as well? In other words, if I get this working so it only displays paid_attendance
if value is greater than 0, I dont want to have "Paid Attendance" still showing up for certain posts with nothing next to it (the posts where paid_attendance
= 0). Thanks in advance.
<?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:
Changed ( '>0' == $unformatted )
to ( $unformatted >0 )
and now its working. However, would appreciate if anyone has insight on my note above regarding only displaying the h4
text if the value is >0 as well. thanks
Update: This does the trick:
elseif ( $unformatted >0 ) {
echo '<h4 class="paid_tix">Paid Tickets: '; echo '</h4>';echo ' '; echo '<h4 class="integer">'; echo $integerValue; echo '</h4>';
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:
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.if
statement didn't do anything, so you can omit it.$integerValue
as it's an int
instead of $unformatted
or $cleaned
since you're doing a numeric comparison.
$unformatted
were the string 'a'. In PHP, zero is not less than 'a':php > var_dump( 0 < 'a' );
bool(false)
$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>';
}
Description: