How do I display a “category products drop down in a wordpress page” in Woocommerce 2.5.2

前端 未结 3 515
北海茫月
北海茫月 2021-01-28 09:08

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

 


        
相关标签:
3条回答
  • 2021-01-28 09:34
    <?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.!

    0 讨论(0)
  • 2021-01-28 09:41

    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.

    0 讨论(0)
  • 2021-01-28 09:42

    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 讨论(0)
提交回复
热议问题