How can I use custom field values with PHP in the template editor?

Sometimes you want to use values from a custom field, but you need to transform them by PHP. So for example, you have a price field and the price is 9$ in another case 9.95$. You want to display them always in the same format. So, 9.00 $ and 9.95 $.

If you try to realize this with the template tags like here, it won’t work :

<?php
echo number_format( #meta_price#, 2, '.' ) . '$';
?>

Instead, you have to use the WordPress function get_post_meta, like here:

<?php
$price = get_post_meta( get_the_ID(), 'price', true );
echo number_format( $price, 2, '.' ) . '$';
?>

Use our search to find more help:

Search
Category
Tag

© 2014, websupporter | Credits

This is a demo store for testing purposes — no orders shall be fulfilled.