【问题标题】:Displaying all products categories in a drop down menu在下拉菜单中显示所有产品类别
【发布时间】:2016-02-05 21:54:52
【问题描述】:

使用 WP 和 WooCommerce 产品类别。我正在尝试在下拉菜单中显示所有产品类别。我有这个工作,但是我还需要显示层次结构。例如,苹果是水果的一个子类别。

所以我希望显示如下所示:

Fruit    
-Apple   
-Pear

但目前看起来是这样的:

 Fruit    
 Apple   
 Pear

我的代码目前如下所示:

$args = array(
            'number'     => $number,
            'orderby'    => $orderby,
            //'name' => 'select_name',
            //'id' => 'select_name',
            'order'      => $order,
            'hide_empty' => false,
            'include'    => $ids,
            'hierarchical' => true
            );

$product_categories = get_terms( 'product_cat', $args );

我的标记:

<select name='categorylist'>    
   <?php foreach($product_categories as $cat) {
      echo "<option value='{$cat->name}'>{$cat->name}</option>"; 
   } ?>

我知道可以为子类别设置这样的层次结构。谁能建议我可以对我的 $args 数组做些什么来实现这一点。谢谢

【问题讨论】:

  • 您只需要检查特定的$product_categories 元素是否具有'hierarchical' == true 值。我假设你可以查询这个。
  • 我在这里将其拉回到几百个类别,因此需要即时设置。谢谢

标签: php wordpress woocommerce


【解决方案1】:

为什么不使用wp_dropdown_categories()

$args = array(
    'selected'           => 0,
    'hierarchical'       => 1, 
    'taxonomy'           => 'product_cat',
)
wp_dropdown_categories( $args ); 

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2012-01-11
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多