显示特定类别的随机产品缩略图
display Random product thumbnails for specific categories
我正在尝试创建一个如下所示的动态趋势类别框:
// 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);
原始出处:
不幸的是,上面的代码只针对一个类别,我需要的是在每行中添加 8 个类别框 4 列,并为同一类别显示随机图像 x 3。
我试过但没有找到更好的插件或代码。
您必须循环您的类别并为该类别中的产品构建查询。
这是我的解决方案 - [random_thumbnail cat_ids='1,2,3,4,5,6,7,8']
if( !function_exists('custom_shortcode_random_thumbnail') ) {
function custom_shortcode_random_thumbnail( $atts ) {
// Shortcode attributes
$atts = shortcode_atts(
array(
'cat_ids' => '', // category ids
'size' => 'shop_thumbnail', // Default image size
),
$atts, 'random_thumbnail'
);
$args = array(
'orderby' => 'rand',
'hide_empty' => true,
'include' => $atts['cat_ids']
);
$product_categories = get_terms( 'product_cat', $args );
if($product_categories) {
echo '<div class="category-boxes-wrap">';
foreach($product_categories as $cat) {
$args = array(
'posts_per_page' => 3,
'post_type' => 'product',
'orderby' => 'rand,',
'suppress_filters' => true,
'tax_query' => array(
array(
'taxonomy' => 'product_cat',
'field' => 'slug',
'terms' => $cat->slug,
)
),
);
$product_list = get_posts( $args );
shuffle($product_list); // extra shuffle if you need
if($product_list):
echo '<div class="category-box">';
echo '<div class="category-title"><a href="'.get_category_link($cat->term_id).'">'.$cat->name.'</a></div>';
echo '<div class="cat-prod-count">'.$cat->count.' '.__('products','textdomain').'</div>';
echo '<div class="product-images">';
foreach ( $product_list as $list ) : setup_postdata( $list );
global $product;
echo '<a href="'.get_permalink( $product->get_id() ).'">';
echo $product->get_image();
echo '</a>';
endforeach;
echo '</div>';
echo '</div>';
endif;
wp_reset_postdata();
}
echo '</div>';
}
}
add_shortcode( 'random_thumbnail', 'custom_shortcode_random_thumbnail' );
}
我使用的 css 结果 - https://prnt.sc/1yb239g
.category-boxes-wrap {
display: grid;
grid-template-columns: repeat(4,1fr);
}
.category-box {
border: 1px solid rgb(153, 153, 153);
padding:30px;
}
.category-title,
.cat-prod-count {
text-align: center;
}
.product-images {
display: grid;
grid-template-columns: repeat(3,1fr);
grid-column-gap: 10px;
}
.product-images img {
width: 100%;
height: auto;
}
我正在尝试创建一个如下所示的动态趋势类别框:
// 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);
原始出处:
不幸的是,上面的代码只针对一个类别,我需要的是在每行中添加 8 个类别框 4 列,并为同一类别显示随机图像 x 3。
我试过但没有找到更好的插件或代码。
您必须循环您的类别并为该类别中的产品构建查询。
这是我的解决方案 - [random_thumbnail cat_ids='1,2,3,4,5,6,7,8']
if( !function_exists('custom_shortcode_random_thumbnail') ) {
function custom_shortcode_random_thumbnail( $atts ) {
// Shortcode attributes
$atts = shortcode_atts(
array(
'cat_ids' => '', // category ids
'size' => 'shop_thumbnail', // Default image size
),
$atts, 'random_thumbnail'
);
$args = array(
'orderby' => 'rand',
'hide_empty' => true,
'include' => $atts['cat_ids']
);
$product_categories = get_terms( 'product_cat', $args );
if($product_categories) {
echo '<div class="category-boxes-wrap">';
foreach($product_categories as $cat) {
$args = array(
'posts_per_page' => 3,
'post_type' => 'product',
'orderby' => 'rand,',
'suppress_filters' => true,
'tax_query' => array(
array(
'taxonomy' => 'product_cat',
'field' => 'slug',
'terms' => $cat->slug,
)
),
);
$product_list = get_posts( $args );
shuffle($product_list); // extra shuffle if you need
if($product_list):
echo '<div class="category-box">';
echo '<div class="category-title"><a href="'.get_category_link($cat->term_id).'">'.$cat->name.'</a></div>';
echo '<div class="cat-prod-count">'.$cat->count.' '.__('products','textdomain').'</div>';
echo '<div class="product-images">';
foreach ( $product_list as $list ) : setup_postdata( $list );
global $product;
echo '<a href="'.get_permalink( $product->get_id() ).'">';
echo $product->get_image();
echo '</a>';
endforeach;
echo '</div>';
echo '</div>';
endif;
wp_reset_postdata();
}
echo '</div>';
}
}
add_shortcode( 'random_thumbnail', 'custom_shortcode_random_thumbnail' );
}
我使用的 css 结果 - https://prnt.sc/1yb239g
.category-boxes-wrap {
display: grid;
grid-template-columns: repeat(4,1fr);
}
.category-box {
border: 1px solid rgb(153, 153, 153);
padding:30px;
}
.category-title,
.cat-prod-count {
text-align: center;
}
.product-images {
display: grid;
grid-template-columns: repeat(3,1fr);
grid-column-gap: 10px;
}
.product-images img {
width: 100%;
height: auto;
}