Php 产量仅在一系列价格中最低和最高
我在我的Wordpress网站上保存了大量定制的post-type商店商品,并以字符串形式将各种价格作为定制元附加到Php 产量仅在一系列价格中最低和最高,php,wordpress,Php,Wordpress,我在我的Wordpress网站上保存了大量定制的post-type商店商品,并以字符串形式将各种价格作为定制元附加到$nn.nn或干脆$n 我想输出以下内容: 价格从英镑nn.nn到英镑nn.nn 所以,我想查询价格元,只显示最低和最高 是否可以在PHP中执行此操作 到目前为止,我只想到了这一点,但它只输出一个以逗号分隔的所有价格列表: $args = array('post_type' => 'shop-items',); $the_query = new WP_Query( $arg
$nn.nn
或干脆$n
我想输出以下内容:
价格从英镑nn.nn到英镑nn.nn
所以,我想查询价格元,只显示最低和最高
是否可以在PHP中执行此操作
到目前为止,我只想到了这一点,但它只输出一个以逗号分隔的所有价格列表:
$args = array('post_type' => 'shop-items',);
$the_query = new WP_Query( $args );
if ( $the_query->have_posts() ):
echo 'Prices range from ';
// The Loop
while ( $the_query->have_posts() ) : $the_query->the_post();
$price = get_post_meta( get_the_ID(), 'itemprice', true );
echo '$price . ', ';
endwhile;
endif;
首先声明变量(将包含结果): 在您的循环中,请执行以下操作:
$price = get_post_meta( get_the_ID(), 'itemprice', true );
if ($price < $min_price)
$min_price = $price;
if ($price > $max_price)
$max_price = $price;
对价格数组使用sort()
if ( $the_query->have_posts() ):
$pricearray=array();
echo 'Prices range from ';
// The Loop
while ( $the_query->have_posts() ) : $the_query->the_post();
$price = get_post_meta( get_the_ID(), 'itemprice', true );
$price=str_replace("£","",$price);
if(!empty($price)){
$pricearray[]= $price;
}else{
$pricearray[]= "0";
}
endwhile;
endif;
sort($pricearray, SORT_NUMERIC);
$minprice=$pricearray[0];
$maxprice=$pricearray[count($pricearray)-1]
谢谢,这是为了显示最高的价格,但由于某些原因不是最低的。有什么想法吗?我尝试在开始时更改if($price write
error\u reporting(0);ini\u set('display\u errors','On');
,并告诉我是否有任何差异…?(任何错误)我是否应该在error\u reporting之前放置'echo'?当我删除echo时,它不显示任何提示,我尝试回显$minprice和$maxprice,但什么都看不到(都是空的).有什么想法吗?print\r($pricearray)
看看你在数组中得到了什么还是空数组我打印时得到了1,而在while循环中你看到了多少值我看到的都是1,最后是1
echo "$min_price to $max_price";
if ( $the_query->have_posts() ):
$pricearray=array();
echo 'Prices range from ';
// The Loop
while ( $the_query->have_posts() ) : $the_query->the_post();
$price = get_post_meta( get_the_ID(), 'itemprice', true );
$price=str_replace("£","",$price);
if(!empty($price)){
$pricearray[]= $price;
}else{
$pricearray[]= "0";
}
endwhile;
endif;
sort($pricearray, SORT_NUMERIC);
$minprice=$pricearray[0];
$maxprice=$pricearray[count($pricearray)-1]