vendredi 5 février 2016

Displaying all products categories in a drop down menu

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



via Chebli Mohamed

Aucun commentaire:

Enregistrer un commentaire