【问题标题】:Display a random product thumbnail for a product category in WooCommerce在 WooCommerce 中显示产品类别的随机产品缩略图
【发布时间】:2017-08-28 15:39:23
【问题描述】:

我正在尝试提取随机产品缩略图以在我的一个页面上显示为图像。我似乎找不到可行的方法,并尝试了 thisthis 帖子中的解决方案。

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

这是我目前正在尝试的,但我仍然不确定如何做到这一点。

functions.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);

【问题讨论】:

    标签: php wordpress woocommerce categories product


    【解决方案1】:

    好的,经过一些测试,我让它以不同的模块化方式工作。我创建了一个自定义简码,它根据产品类别随机显示一个产品缩略图**。

    这个简码有 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 文件或任何插件文件中。

    此代码已经过测试并且可以工作

    【讨论】:

    • 谢谢!我已经转移到一些电子表格工作,但会尽快测试并标记为正确
    • 如何查看库存状态并显示有库存的随机产品
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2014-04-06
    • 2017-05-24
    • 1970-01-01
    • 2019-01-24
    • 1970-01-01
    相关资源
    最近更新 更多