Support

Account

Home Forums Backend Issues (wp-admin) Alter field output

Solved

Alter field output

  • I have a bit of code:

    
    <?php
    			$singleservice = get_field('single_service_repeater');
    			if ($singleservice) {
    				echo '<div class = "row">';
    				foreach ($singleservice as $row) {
    					$info = $row['service_information'];
    					$price = $row['service_price'];
    					$price = '$' .$price;
    					echo '<div class = "singleservice clearfix">
    						<div class = "text">'
    							.$info.
    						'</div><!--text-->
    						<div class = "price">'
    							.$price.
    						'</div><!--price-->
    					</div><!--singleservice-->';
    				}
    				echo '</div><!--row-->';
    			}
    			?>
    

    it adds a dollar sign before a price that is set in a field on the backend. However I have 2 places that I’d like to not have the $sign added to the output. 2 items that I’d like “Call for Price”. Can I suppress the $ sign from the above code for these 2 items in the field settings of the backend? * I just noticed the 8th line of above code doesn’t display properly, it should read $price = ‘$’ .$price; – I’m not sure why it shows properly on the edit screen then after saving shows as above (in post).

  • Hi @buzzbored

    I believe you can set the price to empty if you want it to be “Call for Price” and then check it in the template like this:

    $price = $row['service_price'];
    if( $price ){
        $price = '$' . $price;
    }

    I hope this helps.

  • Accidently Struck wrong key – don’t know how to clear this

  • I’m not familiar with ACF but, how would the above output ‘Call for Price’ I don’t see a check for anything in the if statement or an alternative (elseif, else) if there isn’t a numeric value entered. Is this a built in function?
    Sorry if this is simple, I might be dense, it is early here.
    But wouldn’t you need to do something like:

    
    $price = $row['service_price'];
    if( $price > 0 ){
        $price = '$' . $price;
    }
    else {
         $price = 'Call for Price';
    }
    
  • Hi @buzzbored

    I’m sorry I thought you only want to remove the ‘$’ sign if you don’t set the price. IF you want to show the ‘Call for Price’ text, I think your code is perfect.

    Hope this clears things up 🙂

  • Thank you – I’m unfamiliar with the workings of the acf. Once I’m back on that project I’m waiting for cpanel access (the particular file is locked down, I need to change some permissions temporarily), once I look at the changes – I’ll come back and give you your props.

  • I would like to thank James. I didn’t know if there where any acf nuances to worry about when changing the above code, besides it having been quite awhile since I’ve done any coding (many years). Thanks to him the changes went off without a hitch, slick enter 0 or nothing in the form field on the backend and ‘Call for Service’ displays. Enter any number greater than 0 and it displays the $ + the number (EX:$125).

Viewing 7 posts - 1 through 7 (of 7 total)

You must be logged in to reply to this topic.