golaz
golaz

Reputation: 71

Custom Field function wordpress

How create corect function with this code?

<?php $price = get_post_meta($id, 'price_input', true);

    if ($price != ''){
        echo $price . " Euro";
    }
    else {
        echo "None";
    }
 ?>

I want to put this code in function.php or another place and use <?php myfunction(); ?>

Thx for answer!

Upvotes: 0

Views: 57

Answers (1)

RRikesh
RRikesh

Reputation: 14381

That's pretty straight forward PHP:

#in functions.php
function display_price( $post_id ){
    $price = get_post_meta($post_id, 'price_input', true);

    if ($price != ''){
        echo $price . " Euro";
    }
    else {
        echo "None";
    }
}

in your template:

<?php display_price( $id ); ?>

Upvotes: 1

Related Questions