获取完整的产品一个产品类别的单页上WooCommerce
问题描述:
单后
我要寻找一个代码来显示完整的产品信息(产品名称,购买按钮,图片库,描述等)全部产品我在一个单一职位上选择的特定产品类别。获取完整的产品一个产品类别的单页上WooCommerce
没有实际上这方面的任何短代码,我不具备的技能由我自己写的东西。
也许有人有一个解决方案?
答
这是一个自定义短代码在循环中使用现有的[product_page]
woocommerce短代码输出定义产品类别(一个接一个)的所有单个产品页面。
这种风俗简码有2个参数:
-
cat
的产品类别的(塞) -
limit
职位的数量要显示(默认是全部)
的代码:
if(!function_exists('single_product_pages_by_category')) {
// Add Shortcode
function single_product_pages_by_category($atts) {
// Attributes
$atts = shortcode_atts(
array(
'cat' => '',
'limit' =>'-1'
),
$atts, 'product_pages_cat'
);
$posts = get_posts(array(
'post_type' => 'product',
'post_status' => 'publish',
'posts_per_page' => $atts['limit'],
'tax_query' => array(array(
'taxonomy' => 'product_cat',
'field' => 'slug',
'terms' => $atts['cat'],
))
));
$output = '<div class="products-in-'.$atts['cat'].'">'; // DIV container html tag (opening)
// Iterating though each product in the defined category
foreach($posts as $post){
$post_id = $post->ID; // The Product ID
// Using WooCommerce [product_page] existing shortcode in the loop
$output .= '<div class="product-'.$post_id.'">' . do_shortcode ("[product_page id=$post_id]") . '</div>';
}
$output .= '</div>'; // DIV container html tag (closing)
// Always return the output (never echo)
return $output;
}
add_shortcode('product_pages_cat', 'single_product_pages_by_category');
}
代码放在您的活动子主题的function.php文件(活动的主题或任何插件文件)。
测试和WooCommerce 3+工作
用法(例如,对于 '海报' 的产品类别蛞蝓):
[product_pages_cat cat="posters"]
是那正是我所期待的。 – benrolf