我正在尝试获取一些数值,并将其恢复,这要归功于:wp_get_post_terms,它检索定制分类法的子分类法,其中价格以数字形式插入到中。
我们的目标是按照决定的顺序使用sort和rsort显示价格。
但是,在恢复值时,将元素添加到数组并在以后对它们进行排序是错误的
代码:
foreach ($qprice as $r)
{
$children = $r->term_id;
$child_terms = get_term_children($r->term_id, 'servizi_pro');
$all_terms = wp_get_post_terms($r->ID, 'servizi_pro');
foreach ( $all_terms as $term )
{
if (!in_array($term->term_id, $child_terms))
continue;
$price = $term->name;
$final_price = [$price];
}
rsort($final_price, SORT_NUMERIC);
foreach($final_price as $pr_f) {
echo '<span class="numberCircle"><span>'.str_pad($pr_f, 9).''.'€'.'</span></span>';
}
}另外:
$final_price = [];
foreach ($qprice as $r)
{
$children = $r->term_id;
$child_terms = get_term_children($r->term_id, 'servizi_pro');
$all_terms = wp_get_post_terms($r->ID, 'servizi_pro');
foreach ( $all_terms as $term )
{
if (!in_array($term->term_id, $child_terms))
continue;
$price = $term->name;
$final_price[] = $price;
}
}
sort($final_price, SORT_NUMERIC);
foreach($final_price as $pr_f) {
echo '<span class="numberCircle"><span>'.str_pad($pr_f, 9).'€'.'</span></span>';
}发布于 2020-11-11 02:12:48
您需要在内部foreach循环之前初始化$final_price数组,并推送到数组上,而不是每次循环都替换它。
foreach ($qprice as $r)
{
$children = $r->term_id;
$child_terms = get_term_children($r->term_id, 'servizi_pro');
$all_terms = wp_get_post_terms($r->ID, 'servizi_pro');
$final_price = [];
foreach ( $all_terms as $term )
{
if (!in_array($term->term_id, $child_terms))
continue;
$price = $term->name;
$final_price[] = $price;
}
rsort($final_price, SORT_NUMERIC);
foreach($final_price as $pr_f) {
echo '<span class="numberCircle"><span>'.str_pad($pr_f, 9).''.'€'.'</span></span>';
}
}https://stackoverflow.com/questions/64774425
复制相似问题