• Hello all,

    I am finishing up on a client’s website.
    They sell classic cars, so there are some prices they do not wish to displose online.

    I have been able to customize heavily the metaboxes which came with the metaboxes already included in the theme to do exactly what I wanted, but this is the last function I haven’t been able to implement.

    What i think is best for them is that, if they leave the price field empty, the sentence ‘P.O.A.’ would display automatically (as adding a checkbox which hides the price would be probably too heavy code-wise).

    Currently, it shows a basic “<?php listing_price(); ?>” to get the price. If knwo I need an if and else function here, but I don’t know how to code it.

    Thank you for your help.
    Best,
    Yann

Viewing 4 replies - 1 through 4 (of 4 total)
  • I think you can use code like this:

    <?php
       $empty_price = '';
       ob_start();
          listing_price();
       $price = ob_get_clean();
       if ( $price == $empty_price ) {
          echo 'P.O.A';
       } else {
          echo $price;
       }
    ?>

    You must set $empty_price to exactly what is returned from listing_price() when the price is empty before this will work. Does it return an empty string, or is there some HTML included?

    Thread Starter yjules

    (@yjules)

    Hello there,

    Thank you for your reply. I’ve tried it, and I still get the same “0” displayed by default if the field is empty.
    I think it is because displaying this 0 is embeded in the functions of the theme, but it is in the same field as the formatting for the numbers.

    This is what I have in my functions-theme.php file:

    /*-----------------------------------------------------------------------------------*/
    
    /* Listing Price */
    
    /*-----------------------------------------------------------------------------------*/
    
    function listing_price() {
    	global $post;
    	$price_meta = get_post_meta(get_the_ID(), '_ct_price', true);
    	$price_meta= preg_replace('/[\$,]/', '', $price_meta);
    	echo  number_format($price_meta, 0, ',', '.');
    }

    Did you change the value of $empty_price in the code I sent? It must match exactly what the listing_price() function returns for an empty price. It may be just a single zero or something more. Try changing $empty_price to a zero, like this:

    $empty_price = '0';

    Of course, you could always modify the function in functions.php, but I hesitated to do that because I did not know if it might be used in other places, and you might not want the same operation there.

Viewing 4 replies - 1 through 4 (of 4 total)
  • The topic ‘Insert a default text if custom field is empty’ is closed to new replies.