WP - woocommerce按订单列出产品类别

时间:2013-09-24 07:05:51

标签: php wordpress sorting woocommerce categories

我第一次使用(wordpress)woocommerce。

我正在尝试以树形状显示所有产品类别及其各自的子类别,但我似乎无法按照我使用拖放在后端订购它们的方式对它们进行排序。

WP get_terms()只允许按id,name,count,slug和none进行排序。

我正在使用的代码是:

<?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>

3 个答案:

答案 0 :(得分:1)

我仍然没有找到使用代码解决此问题的方法,因此我将只使用自定义菜单,然后从那里添加产品类别。

答案 1 :(得分:0)

我有一个使用默认woo功能且没有其他插件的解决方案。 第一: get_woocommerce_term_meta($ sub_category-> 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 );

答案 2 :(得分:0)

我意识到自从发布此问题以来已经有一段时间了,但是今天我遇到了相同的问题并以这种方式解决了

$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;
});
相关问题