Reputation: 557
I am using custom fields to display prices (except for the dollar sign). My goal is to code the dollar sign within the theme, but I encountered an issue.
The reason I am using this approach is because if I enter a dollar sign in the custom field I will not be able to use 'orderby' => 'meta_value_num'
And I need to use a conditional statement to display the dollar sign because not all posts will have prices.
The code below results in
$ 9.75
Notice the white space between the dollar sign and the number "9". How can I remove the white space? Or is there an alternative method to code a dollar sign using a conditional statement ?
<?php if( get_post_meta($post->ID, 'price', true) ) { ?>
$
<?php } ?>
<?php echo get_post_meta( get_the_ID(), 'price', true); ?>
Upvotes: 0
Views: 604
Reputation: 37701
Removed the whitespaces in the code itself and optimized it a bit (didn't understand why would you have a condition for the dollar sign, but not for the price itself), this should work:
<?php if( get_post_meta($post->ID, 'price', true) ) {
echo '$', get_post_meta( get_the_ID(), 'price', true);
} ?>
Where was space coming from in the original code:
<?php if( get_post_meta($post->ID, 'price', true) ) { ?>
$<!- HERE ->
<?php } ?> <!- HERE ->
<!- AND HERE ->
<?php echo get_post_meta( get_the_ID(), 'price', true); ?>
See the idea here: http://jsfiddle.net/93mcu7sj/
Upvotes: 2