Created
May 26, 2017 02:36
-
-
Save westcoastdigital/3e5ab86d05eb7c260b71c4753393409c to your computer and use it in GitHub Desktop.
Custom loop args gro WooCommerce featured products
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| <?php | |
| function excerpt($limit) { | |
| $excerpt = explode(' ', get_the_excerpt(), $limit); | |
| if (count($excerpt)>=$limit) { | |
| array_pop($excerpt); | |
| $excerpt = implode(" ",$excerpt).'...'; | |
| } else { | |
| $excerpt = implode(" ",$excerpt); | |
| } | |
| $excerpt = preg_replace('`\[[^\]]*\]`','',$excerpt); | |
| return $excerpt; | |
| } | |
| function content($limit) { | |
| $content = explode(' ', get_the_content(), $limit); | |
| if (count($content)>=$limit) { | |
| array_pop($content); | |
| $content = implode(" ",$content).'...'; | |
| } else { | |
| $content = implode(" ",$content); | |
| } | |
| $content = preg_replace('/\[.+\]/','', $content); | |
| $content = apply_filters('the_content', $content); | |
| $content = str_replace(']]>', ']]>', $content); | |
| return $content; | |
| } | |
| $meta_query = WC()->query->get_meta_query(); | |
| $tax_query = WC()->query->get_tax_query(); | |
| $tax_query[] = array( | |
| 'taxonomy' => 'product_visibility', | |
| 'field' => 'name', | |
| 'terms' => 'featured', | |
| 'operator' => 'IN', | |
| ); | |
| $args = array( | |
| 'post_type' => 'product', | |
| 'post_status' => 'publish', | |
| 'ignore_sticky_posts' => 1, | |
| 'posts_per_page' => $atts['per_page'], | |
| 'orderby' => $atts['orderby'], | |
| 'order' => $atts['order'], | |
| 'meta_query' => $meta_query, | |
| 'tax_query' => $tax_query, | |
| ); | |
| $featured_query = new WP_Query( $args ); | |
| if ($featured_query->have_posts()) : | |
| echo '<div class="product-carousel">'; | |
| while ($featured_query->have_posts()) : | |
| $featured_query->the_post(); | |
| $product = get_product( $featured_query->post->ID ); ?> | |
| <div class="product-card"> | |
| <div class="row"> | |
| <div class="thumbnail col-6"> | |
| <?php if ( has_post_thumbnail() ) { | |
| the_post_thumbnail(); | |
| } ?> | |
| </div> | |
| <div class="content-wrapper col-6"> | |
| <h2 class="title"><?php the_title(); ?></h2> | |
| <p class="excerpt"><?php excerpt(16);?> </p> | |
| <div class="button"> | |
| <a href=""><?php echo __('Buy now', 'rxmedica'); ?></a> | |
| </div> | |
| </div> | |
| </div> | |
| </div> | |
| <?php endwhile; | |
| echo '</div>'; | |
| endif; | |
| wp_reset_query(); | |
| ?> |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment