Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- <?php
- // show free products in the shop
- // Use: [free_products columns="3" orderby="title" order="asc"]
- // code goes in functions.php for your child theme
- add_shortcode('free_products', 'free_products');
- function free_products( $atts ) {
- $atts = shortcode_atts( array(
- 'columns' => '4',
- 'orderby' => 'title',
- 'order' => 'asc',
- ), $atts );
- $query_args = array(
- 'post_type' => 'product',
- 'post_status' => 'publish',
- 'ignore_sticky_posts' => 1,
- 'orderby' => $atts['orderby'],
- 'order' => $atts['order'],
- 'posts_per_page' => 12,
- 'meta_query' => array(
- array(
- 'key' => '_price',
- 'value' => 0,
- 'compare' => '=',
- 'type' => 'NUMERIC'
- )
- )
- );
- $loop = new WP_Query($query_args);
- print '<div class="clear"></div>';
- print '<ul class="products-list">';
- if ( $loop->have_posts() ) {
- while ( $loop->have_posts() ) : $loop->the_post();
- wc_get_template_part( 'content', 'product' );
- endwhile;
- } else {
- echo __( 'No products found' );
- }
- print '</ul>';
- print '<div class="clear"></div>';
- wp_reset_postdata();
- return;
- }
Add Comment
Please, Sign In to add comment