Custom Field function wordpress

2019-09-03 08:03发布

问题:

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!

回答1:

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 ); ?>


标签: wordpress