繁体   English   中英

在WooCommerce的单个帖子中获取产品类别的完整产品单页

[英]Get full products single pages for a product category on a single post in WooCommerce

我正在寻找一种代码,以在单个帖子中显示我选择的特定产品类别的所有产品的完整产品信息(产品标题,购买按钮,图片库,描述等)。

实际上没有任何短代码,而且我没有自己写东西的技能。

也许有人对此有解决方案?

这是一个自定义的简码,它在循环中使用现有的[product_page] woocommerce简码,以输出已定义产品类别的所有单个产品页面(一个接一个)。

这个自定义的简码有2个参数:

  • 产品类别的cat (sl)
  • 显示的帖子数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"]

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM