我已经按城市和地区建立了分类。
在存档页面上,它显示得很好。
问题是,视图是按字母顺序排列的。
例如:
现展示:
我要:
有人能帮我改变一下吗?
对不起,如果我的英语不好。谢谢
我创建了这样一个片段:
add_action('woocommerce_loop_add_to_cart_link','displaying_product_attributes',5);
function displaying_product_attributes () {
global $product;
$harga_val = $product->get_attribute('pa_harga');
$kt_val = $product->get_attribute('pa_kt');
$km_val = $product->get_attribute('pa_km');
$luas_bangunan_val = $product->get_attribute('pa_luas-bangunan');
$luas_tanah_val = $product->get_attribute('pa_luas-tanah');
$terms = get_the_terms( $product->get_id(),'product_cat' );
if( $terms ) {
$names = array();
foreach ( $terms as $term ) {
$names[] = $term->name;
}
}
echo "".$harga_val."";
echo " ".$kt_val." ".$km_val." ".$luas_bangunan_val."m2 ".$luas_tanah_val."m2";
print ' '.join( ', ', $names ).''.PHP_EOL;;
}让我告诉你我想要什么。
在设置存档页/存储页上,我只显示标题、图像和添加到购物车按钮。(在我编辑的“添加到购物车”按钮上)
我在“添加到购物车”按钮之前插入了一些属性和类别。
乍一看上去不错,但在信息类别上就成了倒置。(按字母顺序排列),然后就变得不统一了。
我希望你或其他人仍然愿意帮助我,
上面的代码片段显示了下面的结果

发布于 2021-08-25 16:21:08
要按层次进行排序,请尝试向functions.php (或插件)添加排序函数。
// This will sort and place everything in the $into array
function sort_terms_hierarchically(Array &$cats, Array &$into, $parentId = 0) {
foreach ($cats as $i => $cat) {
if ($cat->parent == $parentId) {
$cat->posts = array();
$into[$cat->term_id] = $cat;
unset($cats[$i]);
}
}
foreach ($into as $topCat) {
$topCat->children = array();
sort_terms_hierarchically($cats, $topCat->children, $topCat->term_id);
}
}现在,在显示端,您可以抓取所有类别并以父名第二的形式显示它们。
$terms = get_the_terms( $product->get_id(), 'product_cat' );
if ( $terms ) {
$listItems = '';
$sortedTerms = array(); // empty array to hold the sorted terms
sort_terms_hierarchically( $terms, $sortedTerms ); // sort everything
foreach( $sortedTerms as $parent) {
if ( empty( $parent->children ) ) continue;
foreach( $parent->children as $child ) {
$listItems .= '' . $child->name . ', ' . $parent->name . '';
}
}
}
// Display the list
if ( !empty( $listItems ) ) {
echo '' . $listItems . '';
}希望这能更好地回答你的问题!
https://wordpress.stackexchange.com/questions/393762
复制相似问题