Change default sorting for specific Woocommerce product category archive pages

前端 未结 3 917
孤城傲影
孤城傲影 2020-12-22 02:12

I need to change the default product sorting option to \"Newness\" for a specific product category on my site. I know you can go to WooCommerce > Settings > Product > Displa

3条回答
  •  攒了一身酷
    2020-12-22 02:59

    Assuming you're working within a custom page template, this should be relatively easy. Not entirely sure if your approach will work with utilizing is_page() to force the sorting of the product to change, however... You can utilize the global WP_Query to create a custom query, sorting your products however you'd like. Here's an example:

     'product',
            'posts_per_page' => -1, //or... set your post per page and utilize pagination if required.
            'orderby'        => 'title',
            'order'          => 'ASC',
        );
    
        $loop = new WP_Query( $args );
    
        while ( $loop->have_posts() ) : $loop->the_post();
            global $product;
    
            //this is where you can output your products
            //which will be ordered according to the set arguments above.
    
        endwhile;
    
        wp_reset_query();
    ?>
    

    EDIT:

    If you're looking for this kind of functionality for a specific category, I would simply create a new page in your system and use the WooCommerce product short-code. You don't need to add anything in your functions.php or hook onto anything this way. The following short-code is an example you can use to...for example order your products on a specific category page by title.

    [products category="yourcategory" orderby="title" ]
    

提交回复
热议问题