简体   繁体   English

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

[英]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.

相关问题 Woocommerce中产品类别归档页面和相关单品的条件逻辑 - Conditional logic for product category archive pages and related single products in Woocommerce 在WooCommerce可变产品单页中获取选定的产品变体ID - Get the selected product variation ID in WooCommerce variable products single pages Woocommerce面包屑,用单个产品页面上的页码替换产品类别 - Woocommerce Breadcrumbs, Replace Product Category with Page Number on Single Product Pages 为产品类别替换 Woocommerce 单一产品页面上的“添加到购物车”按钮 - Replace Add to cart button on Woocommerce Single Product Pages for a product category 仅在 WooCommerce 单个产品页面中隐藏某些产品的选项卡 - Hiding tabs only for some products in WooCommerce single product pages 更改 WooCommerce 单个产品页面中的副标题“相关产品” - Change subtitle “Related Products” in WooCommerce single product pages 在单个产品页面上的WooCommerce分页-但仅在父类别中 - WooCommerce pagination on single product pages - but only inside parent category 在Woocommerce单一产品页面中获取产品类别名称和描述 - Get the product category name and description in Woocommerce Single Product page 在正文中获取 WooCommerce 单个产品页面的一些产品数据 - Get some product data in the body for WooCommerce Single product pages 获取特定产品属性并将其显示在Woocommerce单一产品页面中 - Get specific product attribute and display it in Woocommerce Single Product Pages
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM