In WooCommerce, I have created a the following shortcode which works great:
add_shortcode ('andersen_product_slider', 'andersen_product_slider_loop' );
/**
* Create WooCommerce Featured Image Loop Slider
*/
function andersen_product_slider_loop($atts, $content = null) {
extract( shortcode_atts( array(
'title' => 'Featured Products',
'link' => 'Explore',
'linkurl' => 'http://localhost:8888/andersenbeauty',
'featured' => 'IN',
'category' => '',
'brand' => '',
), $atts) );
$categories = explode(',' , $atts['category']);
$brand = explode(',' , $atts['brand']);
ob_start();
// Setup your custom query
$meta_query = WC()->query->get_meta_query();
$tax_query = WC()->query->get_tax_query();
$args = array(
'post_type' => 'product',
'post_status' => 'publish',
'posts_per_page' => -1,
'product_cat' => $category,
'orderby' => 'date',
'order' => 'DESC',
'meta_query' => $meta_query,
'tax_query' => array(
'relation' => 'AND',
array(
'taxonomy' => 'brand',
'field' => 'slug',
'terms' => $brand,
),
array(
'taxonomy' => 'product_visibility',
'field' => 'name',
'terms' => 'featured',
'operator' => $featured,
),
),
);
// ... / ...
However it breaks when the shortcode does not contain a product category term(s) or a product brand term(s).
How can I edit this to show all categories and all brands if its not referenced within the shortcode?
So using:
[andersen_product_slider title="Latest Products" link="Explore" linkurl="#" featured="IN" category="moisturiser" brand="brandone"]
works fine as expected.
But this:
[andersen_product_slider title="Latest Products" link="Explore" linkurl="#" featured="IN"]
breaks and doesn't display anything…
Any help?
I have removed unnecessary 'featured' => 'IN'
shortcode argument, and some other things. Also product category terms are now queried as a "tax query" instead (the right way in WordPress for taxonomy query in a WP_Query
) .
Try the following that should solve your issue:
// Create WooCommerce Featured Image Loop Slider
add_shortcode ('andersen_product_slider', 'andersen_product_slider_loop' );
function andersen_product_slider_loop($atts, $content = null) {
// Extract shortcode attributes
extract( shortcode_atts( array(
'title' => __('Featured Products'),
'link' => __('Explore'),
'linkurl' => home_url(), // <= Changed
'category' => '',
'brand' => '',
), $atts, 'andersen_product_slider' ) );
// Get WooCommerce meta query and tax query arguments (optional)
$meta_query = WC()->query->get_meta_query();
$tax_query = WC()->query->get_tax_query();
$tax_query['relation'] = 'AND';
$tax_query[] = array(
'taxonomy' => 'product_visibility',
'field' => 'name',
'terms' => 'featured',
);
// Category terms
if ( ! empty($category) ) {
$tax_query[] = array(
'taxonomy' => 'product_cat',
'field' => 'slug',
'terms' => explode(',', $category),
);
}
// Brand terms
if ( ! empty($brand) ) {
$tax_query[] = array(
'taxonomy' => 'brand', // Or "pa_brand" for WooCommerce brand taxonomy
'field' => 'slug',
'terms' => explode(',', $brand),
);
}
// The WP_Query
$loop = new WP_Query( array(
'post_type' => 'product',
'post_status' => 'publish',
'posts_per_page' => -1,
'orderby' => 'date',
'order' => 'DESC',
'meta_query' => $meta_query,
'tax_query' => $tax_query,
) );
ob_start(); // Start buffering (to be use only for printed or echoed content)
// … HERE the rest of your code …
return ob_get_clean(); // return buffered content
}
It should better work with:
[andersen_product_slider title="Latest Products" linkurl="#" category="moisturiser" brand="brandone"]
or also with (no brand or/and no category) :
[andersen_product_slider title="Latest Products" linkurl="#"]
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.