簡體   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