我第一次使用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>发布于 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
复制相似问题