I would like to display a drop down menu for products in a category.
<?php
$args = array(
'order' => 'ASC',
'hide_empty' => $hide_empty,
'include' => $ids,
'posts_per_page' =>'-1'
);
$product_categories = get_terms( 'product_cat', $args );
echo "<select>";
foreach( $product_categories as $category ){
echo "<option value = '" . esc_attr( $category->slug ) . "'>" . esc_html( $category->name ) . "</option>";
}
echo "</select>";
?>
Check this out. This is the way to get product categories.!
Okay so here is how I solved it, with the help of Hemnath mouli, I already gave you the credit for the answer but I wanted to publish the products inside a category in a dropbox.
$args = array(
'posts_per_page' => -1,
'product_cat' => 'motherboard',
'post_type' => 'product',
'orderby' => 'title',
);
$products = new WP_Query( $args );
echo "<select>";
foreach ( $products as $product ) {
$products->the_post();
?>
<option value="<?php the_permalink(); ?>"> <?php the_title(); ?>
<?php
}
echo "</select>";
?>
Now I will need to show the image of this product after selecting it.
You can also use the function wp_dropdown_categories to make your code simpler. To get a dropdown of categories of products you can write like this.
$args = array('hide_empty'=> 0,
'taxonomy'=> 'product_cat',
'hierarchical'=>1);
wp_dropdown_categories($args);
Or if you want to hold the output in a variable you can use the argument 'echo'=>0 and then echo the variable to get the same output.
$args = array('hide_empty'=> 0,
'taxonomy'=> 'product_cat',
'hierarchical'=>1,
'echo'=>0);
$cats = wp_dropdown_categories($args);
echo $cats;