在WooCommerce中显示产品类别的随机产品缩略图 [英] Display a random product thumbnail for a product category in WooCommerce

查看:155
本文介绍了在WooCommerce中显示产品类别的随机产品缩略图的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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

I am trying to pull a random product thumbnail to display as an image on one of my pages. I can't seem to find a way that works, and have tried the solutions from this and this post.

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

It would be beneficial to echo it out in a div as well.

这是我目前正在尝试的方法,但是我仍然不确定如何执行此操作。

Here is what I am currently trying but I'm still unsure how to do this.

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


推荐答案

好了,经过一些测试,我发现它可以在其他环境下工作模块化的方式。我创建了一个自定义的简码,可以根据产品类别**随机显示一个产品缩略图。

Ok after some testing I got it working in a different modular way. I have created a custom shortcode that displays randomly one product thumbnail based on a product category**.

此简码有2个参数:


  • 产品类别:

  • 图像 size (可以是:'shop_thumbnail''shop_catalog''shop_single'

  • The product category slug: cat
  • The image size (can be: 'shop_thumbnail', 'shop_catalog' or 'shop_single')

然后,我在您的自定义函数中使用此短代码,将其链接到 wp_footer 操作钩。

Then I use this short code in your custom function hooked in wp_footer action hook.

这是代码:

// 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文件中,或者

此代码已经过测试并且有效

This code is tested and works

这篇关于在WooCommerce中显示产品类别的随机产品缩略图的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆