我正在建立一个网站,其中多个价格都存储在自定义字段中。我想抓住所有这些自定义字段,只显示最低的数字。
Php是我的弱项,除了简单的get_post_meta
我可以通过这些代码致电所有post_meta
<?php echo get_post_meta($post->ID, 'price_save1', true); ?> - returns £499.99
<?php echo get_post_meta($post->ID, 'price_save2', true); ?> - returns £499.99
<?php echo get_post_meta($post->ID, 'price_save3', true); ?> - returns £489.99
<?php echo get_post_meta($post->ID, 'price_save4', true); ?> - returns £424.99
<?php echo get_post_meta($post->ID, 'price_save5', true); ?> - returns £529.99
<?php echo get_post_meta($post->ID, 'price_save6', true); ?>`- returns £589.99
我希望按照从每个price_save#
自定义字段中显示最低数字的方式做点事情
为了学习一些PHP,我猜测我将需要某种数组来收集所有数字,然后执行一些操作以除去除最低位数以外的所有数字。
我已经尝试过,但是到目前为止还没有运气
<?php
$prices = get_post_meta($post->ID, 'price_save1', 'price_save2', 'price_save3'); // get all prices
rsort($prices); // sort in descending order
$min_price = $prices[0]; // lowest
?>
感谢您的帮助!
答案 0 :(得分:0)
想通了...
$post_meta_1 = get_post_meta($post->ID, 'price_save1', TRUE);
$post_meta_2 = get_post_meta($post->ID, 'price_save2', TRUE);
$post_meta_3 = get_post_meta($post->ID, 'price_save3', TRUE);
$post_meta_4 = get_post_meta($post->ID, 'price_save4', TRUE);
$post_meta_5 = get_post_meta($post->ID, 'price_save5', TRUE);
$post_meta_6 = get_post_meta($post->ID, 'price_save6', TRUE);
$post_meta_7 = get_post_meta($post->ID, 'price_save7', TRUE);
$price1=str_replace("£","",$post_meta_1);
$price2=str_replace("£","",$post_meta_2);
$price3=str_replace("£","",$post_meta_3);
$price4=str_replace("£","",$post_meta_4);
$price5=str_replace("£","",$post_meta_5);
$price6=str_replace("£","",$post_meta_6);
$price7=str_replace("£","",$post_meta_7);
$numbers = array($price1, $price2, $price3, $price4, $price5, $price6, $price7);
$min = min($numbers);
$max = max($numbers);