I have to make a Wordpress plugin which adds shortcode for WooCommerce. I want to get products from a specific product category and the maximum number of products to show. The shortcode parameters should be category ID and product limit. I think I should have to use WP_Query
object.
I need to get it look like this:
Shortcode would be like this: [productslist_category="[category_ID]" limit="[product_limit]"]
I used the code below from this answer (thanks to LoicTheAztec):
if( !function_exists('products_list_in_a_product_category') ) {
function products_list_in_a_product_category( $atts ) {
// Shortcode Attributes
$atts = shortcode_atts(
array(
'cat' => '',
'limit' => '4', // default product per page
'column' => '4', // default columns
),
$atts, 'productslist'
);
// The query
$posts = get_posts( array(
'post_type' => 'product',
'posts_per_page' => intval($atts['limit'])+1,
'product_cat' => $atts['cat'],
) );
$output = '<div class="products-in-'.$atts['cat'].'">';
// The loop
foreach($posts as $post_obj)
$ids_array[] = $post_obj->ID;
$ids = implode( ',', $ids_array );
$columns = $atts['column'];
$output .= do_shortcode ( "[products ids=$ids columns=$columns ]" ) . '</div>';
return $output;
}
add_shortcode( 'productslist', 'products_list_in_a_product_category' );}
But I get an error. It says that there is something wrong with the implode function.