簡體   English   中英

在 WooCommerce 中顯示產品類別的隨機產品縮略圖

[英]Display a random product thumbnail for a product category in WooCommerce

我正在嘗試提取隨機產品縮略圖以在我的一個頁面上顯示為圖像。 我似乎無法找到一種方式工作,並試圖從解決這個這個職位。

在 div 中回顯它也是有益的。

這是我目前正在嘗試的,但我仍然不確定如何做到這一點。

函數.php:

function get_random_thumbnails_for_reg(){
    if(is_page(381)){

        $args = array(
            'tax_query' => array(
                array(
                    'taxonomy' => 'product_cat',
                    'field' => 'slug',   
                    'terms' => 'allison-1000-gm-duramax-series'  
                )
            )
        );

        $random_products = get_posts( $args );
        foreach ( $random_products as $post ) : setup_postdata( $post ); 
        ?>
            <div id="randomPic"><a href="<?php the_permalink(); ?>"><?php the_post_thumbnail(); ?></a></div>
        <?php 
         endforeach; 
         wp_reset_postdata();

    }
}
add_action('wp_footer', 'get_random_thumbnails_for_reg', 50);

好的,經過一些測試,我讓它以不同的模塊化方式工作。 我創建了一個自定義短代碼,它根據產品類別隨機顯示一個產品縮略圖**。

這個簡碼有 2 個參數:

  • 產品類別 slug: cat
  • 圖片size (可以是: 'shop_thumbnail''shop_catalog''shop_single'

然后我在wp_footer動作掛鈎中掛鈎的自定義函數中使用此短代碼。

這是代碼:

// Creating a shortcode that displays a random product image/thumbail
if( !function_exists('custom_shortcode_random_thumbnail') ) {

    function custom_shortcode_random_thumbnail( $atts ) {

        // Shortcode attributes
        $atts = shortcode_atts(
            array(
                'cat'   => '', // product category shortcode attribute
                'size'      => 'shop_thumbnail', // Default image size
            ),
            $atts, 'random_thumbnail'
        );

        // Get products randomly (from a specific product category)
        $random_post = get_posts( array(
            'posts_per_page' => 1,
            'post_type' => 'product',
            'orderby'   => 'rand',
            'post_status' => 'published',
            'tax_query' => array( array(
                'taxonomy' => 'product_cat',
                'field' => 'slug',
                'terms' => $atts['cat'],
            ) )
        ) );
        // Get an instance of the WC_Product object
        $product = wc_get_product($random_post[0]->ID);

        // The Product permalink
        $product_permalink = $product->get_permalink();

        // The Product image. Size can be: 1. 'shop_thumbnail', 2. 'shop_catalog' or 3. 'shop_single'
        $product_image = $product->get_image( $atts['size'] );

        // The output
        return '<div id="random-pic"><a href="' . $product_permalink . '">' . $product_image . '</a></div>';
    }
    add_shortcode( 'random_thumbnail', 'custom_shortcode_random_thumbnail' );
}


// Using the shortcode to display a random product image
function get_random_thumbnails_for_reg(){
    // Only for page ID 381
    if( ! is_page( 381 ) ) return;

    echo do_shortcode( "[random_thumbnail cat='clothing']" );
}
add_action('wp_footer', 'get_random_thumbnails_for_reg', 50);

代碼位於活動子主題(或主題)的 function.php 文件或任何插件文件中。

此代碼經過測試並有效

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM