【问题标题】:WP - woocommerce listing product categories by orderWP - woocommerce 按订单列出产品类别
【发布时间】:2013-09-29 06:48:15
【问题描述】:

我是第一次使用(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>

【问题讨论】:

    标签: php wordpress sorting woocommerce categories


    【解决方案1】:

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

    【讨论】:

      【解决方案2】:

      我有一个使用默认 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 );
      

      【讨论】:

        【解决方案3】:

        我意识到这个问题发布已经有一段时间了,但我今天遇到了同样的问题并以这种方式修复了它

        $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;
        });
        

        【讨论】:

          猜你喜欢
          • 2015-07-16
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          • 2022-01-09
          • 2020-07-15
          • 1970-01-01
          • 2022-01-21
          • 1970-01-01
          相关资源
          最近更新 更多