【发布时间】:2020-11-10 18:08:16
【问题描述】:
我正在尝试获取一些数值,感谢: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>';
}
【问题讨论】: