Display ACF field only if value is greater than 0

admin2025-06-03  2

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>';
Share Improve this question edited Feb 8, 2019 at 5:30 James asked Feb 8, 2019 at 5:02 JamesJames 211 silver badge9 bronze badges 4
  • paid_attendance is number or text field? – Vasim Shaikh Commented Feb 8, 2019 at 6:27
  • There is no h4 in your code. Could you show bigger chunk of it so we know what’s wrong with it and what you want to achieve? – Krzysiek Dróżdż Commented Feb 8, 2019 at 6:32
  • paid_attendance is a number field but for some reason, I kept getting an error that it was being read as a string (I encountered this when I was trying to get the comma to work between every 3 digits (ex, 19,000) - hence the reason my code begins with converting the string to a number. Also, sorry about the h4 explanation, at the point I originally asked the question, I had not added that in yet because honestly I was not sure how...I then realized it can just be part of the echo statement. – James Commented Feb 8, 2019 at 16:29
  • also, I want to improve on asking questions here as I value everyone's time. I see my question has been downvoted, can anyone offer any insight on how I could have improved on asking this question? thanks – James Commented Feb 8, 2019 at 17:02
Add a comment  | 

2 Answers 2

Reset to default 0

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)
$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:

  • Instead of $unformatted, You must check numeric value so you get proper output of boolean.
  • You don't need to make multiple echo in single statement bind variable in single statement.
  • if still hide then you must need to check css or jquery. class might be display :none or visibility:hidden.
转载请注明原文地址:http://conceptsofalgorithm.com/Algorithm/1748933128a314937.html

最新回复(0)