[英]Get full products single pages for a product category on a single post in WooCommerce
I am looking for a code to show full product informations (title of the product, buy button, image gallery, description and so on) for all products of a specific product category of my choice on one single post. 我正在寻找一种代码,以在单个帖子中显示我选择的特定产品类别的所有产品的完整产品信息(产品标题,购买按钮,图片库,描述等)。
There is not actually any shortcode for this and I don't have the skills to write something by myself. 实际上没有任何短代码,而且我没有自己写东西的技能。
Maybe someone has a solution for this? 也许有人对此有解决方案?
Here is a custom shortcode That is using the existing [product_page]
woocommerce shortcode in a loop, to output all single product pages for a defined product category (one after the other). 这是一个自定义的简码,它在循环中使用现有的
[product_page]
woocommerce简码,以输出已定义产品类别的所有单个产品页面(一个接一个)。
This custom shortcode has 2 arguments: 这个自定义的简码有2个参数:
cat
for the product category (slug) cat
(sl) limit
for the number of posts to be displayed (default is all) limit
(默认为全部) The code: 编码:
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' );
}
Code goes in function.php file of your active child theme (active theme or in any plugin file). 代码位于您的活动子主题(活动主题或任何插件文件)的function.php文件中。
Tested and Works in WooCommerce 3+ 在WooCommerce中测试和运行3+
USAGE (example for 'posters' product category slug): 用法 (例如“海报”产品类别标签的示例):
[product_pages_cat cat="posters"]
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.