0
votes

I would like to display a drop down menu for products in a category.

<select>
  <option value="CODE HERE">Volvo</option>
</select> 

So according to Wordpress coding..

<?php

// The Query
$the_query = new WP_Query( $args );

// The Loop
if ( $the_query->have_posts() ) {
    echo '<ul>';
    while ( $the_query->have_posts() ) {
        $the_query->the_post();
        echo '<li>' . get_the_title() . '</li>';
    }
    echo '</ul>';
} else {
    // no posts found
}
/* Restore original Post Data */
wp_reset_postdata();

Okay so I investigated further and I am looking to do a single page template according to https://developer.wordpress.org I am using a child theme for Storefront which is called NOVA WP.

To make this "single page template" I copied page.php and renamed it to page-buildit.php

This is Mypage in which I actually editing the code. I did copy the code but it turns out blank

found this: WooCommerce: Create a shortcode to display product categories but my undestanding is we cant do this anymore with the new wordpress version.

3

3 Answers

2
votes
<?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.!

1
votes

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;
0
votes

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.