Custom Field function wordpress

2019-09-03 07:33发布

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!

标签: wordpress
1条回答
太酷不给撩
2楼-- · 2019-09-03 08:00

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 ); ?>
查看更多
登录 后发表回答