我第一次使用woocommerce (wordpress)。
我试图显示所有的产品类别与他们各自的子类别在树的形状,但我似乎无法排序他们的方式,在后端使用拖放。
WP get_terms()只允许按id、名称、计数、段塞和无顺序排序。
我使用的代码是:
<?php $catTerms = get_terms('product_cat', array('hide_empty' => 0, 'parent' => 0)); ?>
<ul>
<?php foreach($catTerms as $catTerm) : ?>
<li><a href="<?php _e(get_permalink($catTerm->slug)); ?>"><?php _e($catTerm->name); ?></a></li>
<?php endforeach; ?>
</ul>发布于 2013-09-25 06:11:52
我仍然没有找到解决这个问题的方法,所以我将使用一个自定义菜单,然后从那里添加产品类别。
发布于 2018-08-28 13:24:12
我有一个解决方案,使用默认的woo功能,没有额外的插件。首先: get_woocommerce_term_meta( $sub>term_id,'order',true )
然后获取所有类别,并使用此顺序对数组进行排序。
$sortedMenu = array(); // new array
// menu var should be get_categories or taxonomy function return
// I also added order key/val in my category/term array item (along with other terms name, id etc)
// Then I sorted them like bellow
foreach( $menu as $key => $item ) $sortedMenu[$key] = $item['order'];
array_multisort( $sortedMenu, SORT_ASC, $menu );发布于 2019-12-10 14:39:57
我意识到这个问题已经有一段时间没有发布了,但我今天遇到了同样的问题,并以这种方式解决了它。
$categories = $this->get_product_categories();
$categories_ordered = [];
foreach ($categories as $category) {
    $meta = get_term_meta($category->term_id);
    $category->position = intval($meta['order'][0]);
    $categories_ordered[] = $category;
}
uasort($categories_ordered, function ($a, $b) {
    return $a->position > $b->position;
});https://stackoverflow.com/questions/18975076
复制相似问题