Take the 2-minute tour ×
WordPress Development Stack Exchange is a question and answer site for WordPress developers and administrators. It's 100% free, no registration required.

I have this custom field for a post:

key: price
value: 2000

I've added this to my loop:

<div class="buyitnow"><?php $price = get_post_meta($post->ID, 'price', true);
if ( $price ) { ?>
Price: $<?php echo number_format($price ,",",",",","); ?>
<?php } else {} ?></div>

But all I'm getting is:

Price: $
share|improve this question
    
where you trying echo this ?? function.php or page template ? –  Furqan Jan 23 '13 at 19:32
    
Was trying to echo this in index.php. I have an issue regarding this but it's a different question so I'd appreciate if you check it out. –  Darwin229 Jan 23 '13 at 20:15
    
you can't get $post->ID on index.php can you show full code of index.php –  Furqan Jan 23 '13 at 21:23
    
@Furquan Why can't you get $post->ID inside an index.php template? –  kaiser Jan 23 '13 at 23:54
    
Because this is blog page if you want to get id of page or post then you need to put this var inside the loop or loop.php. –  Furqan Jan 24 '13 at 8:54

2 Answers 2

up vote 1 down vote accepted

You have number_format wrong. The second parameter should be a number and you have a string-- a comma. What you want is something like

echo number_format($price,2,",",",");
share|improve this answer
    
Thank you both! –  Darwin229 Jan 23 '13 at 19:15

If you haven't, you need to globalize the $post global variable:

global $post;
$price = get_post_meta($post->ID, 'price', true);

If this is the problem, then you should be getting a PHP notice, because $post is undefined unless/until you globalize it. To see such a notice, you should set WP_DEBUG to true in wp-config.php.

Edit

From this comment:

Sorry, but I'm totally new to code, I learned what I wrote in the question above after hours of searching. Can you should me where exactly whould I add this? right after the <?php..?

PHP code must be placed within PHP code tags. For example:

<div class="buyitnow"><?php global $post; $price = get_post_meta($post->ID, 'price', true);
if ( $price ) { ?>
Price: $<?php echo number_format($price ,",",",",","); ?>
<?php } else {} ?></div>

Change this:

<?php $price = get_post_meta($post->ID, 'price', true);

...to this:

<?php global $post; $price = get_post_meta($post->ID, 'price', true);
share|improve this answer
    
Thank you so much for answering quickly! where should I add this? in which file and where? (and I heard that "Oh Boy!") –  Darwin229 Jan 23 '13 at 18:51
    
You need to globalize $post wherever you use it. In your case, you're using it as a parameter in your call to get_post_meta(), so you need to globalize it before that call. –  Chip Bennett Jan 23 '13 at 18:52
    
Sorry, but I'm totally new to code, I learned what I wrote in the question above after hours of searching. Can you should me where exactly whould I add this? right after the <?php..? –  Darwin229 Jan 23 '13 at 18:54
    
See updated answer. –  Chip Bennett Jan 23 '13 at 18:59
    
Hey Chip, it still isn't working. Should I give the URL? I don't want to be banned for spamming. –  Darwin229 Jan 23 '13 at 19:08

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.