Advertisement
vapvarun

Display Services Products using Shortcode

Oct 28th, 2022
1,152
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
PHP 1.64 KB | Software | 0 0
  1. if( ! function_exists('product_service') ) {
  2.  
  3.     // Add Shortcode
  4.     function product_service( $atts ) {
  5.         global $woocommerce_loop;
  6.  
  7.         // Attributes
  8.         $atts = shortcode_atts(
  9.             array(
  10.                 'columns'   => '4',
  11.                 'limit'     => '20',
  12.             ),
  13.             $atts, 'products_service'
  14.         );
  15.  
  16.  
  17.         $woocommerce_loop['columns'] = $atts['columns'];
  18.        
  19.         // The WP_Query
  20.         $products = new WP_Query( array (
  21.             'post_type'         => 'product',
  22.             'post_status'       => 'publish',
  23.             'posts_per_page'    => $atts['limit'],
  24.               'meta_key' => '_wss_type',
  25.             'meta_value' => array('yes'), //'meta_value' => array('yes'),
  26.             'meta_compare' => 'IN' //'meta_compare' => 'NOT IN'
  27.            
  28.         ));
  29.  
  30.         ob_start();
  31.  
  32.         if ( $products->have_posts() ) { ?>
  33.  
  34.             <?php woocommerce_product_loop_start(); ?>
  35.  
  36.                 <?php while ( $products->have_posts() ) : $products->the_post(); ?>
  37.  
  38.                     <?php wc_get_template_part( 'content', 'product' ); ?>
  39.  
  40.                 <?php endwhile; // end of the loop. ?>
  41.  
  42.             <?php woocommerce_product_loop_end(); ?>
  43.  
  44.             <?php
  45.         } else {
  46.             do_action( "woocommerce_shortcode_products_loop_no_results", $atts );
  47.             echo "<p>There is no results.</p>";
  48.         }
  49.  
  50.         woocommerce_reset_loop();
  51.         wp_reset_postdata();
  52.  
  53.         return '<div class="woocommerce columns-' . $atts['columns'] . '">' . ob_get_clean() . '</div>';
  54.     }
  55.  
  56.     add_shortcode( 'products_service', 'product_service' );
  57. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement