du997562 2016-02-05 12:33
浏览 96

在下拉菜单中显示所有产品类别

Working with WP and WooCommerce product categories. I'm attempting to display all product categories in a drop down menu. I have this working, however I need the hierarchy to also be displayed. For example Apple is a Sub Category of Fruit.

So I want the display to look like this:

Fruit    
-Apple   
-Pear

But it currently looks like this:

 Fruit    
 Apple   
 Pear

My code currently looks like this:

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

My markup:

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

I know it is possible to set the hierarchy like this for sub categories. Could anyone suggest what I can do to my $args array to achieve this. Thanks

  • 写回答

1条回答 默认 最新

  • doubu1964 2016-02-05 13:54
    关注

    Why not use wp_dropdown_categories()

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

报告相同问题?

悬赏问题

  • ¥15 自适应 AR 模型 参数估计Matlab程序
  • ¥100 角动量包络面如何用MATLAB绘制
  • ¥15 merge函数占用内存过大
  • ¥15 Revit2020下载问题
  • ¥15 使用EMD去噪处理RML2016数据集时候的原理
  • ¥15 神经网络预测均方误差很小 但是图像上看着差别太大
  • ¥15 单片机无法进入HAL_TIM_PWM_PulseFinishedCallback回调函数
  • ¥15 Oracle中如何从clob类型截取特定字符串后面的字符
  • ¥15 想通过pywinauto自动电机应用程序按钮,但是找不到应用程序按钮信息
  • ¥15 如何在炒股软件中,爬到我想看的日k线