1
目前,我的主題在主頁上顯示最近添加的WooCommerce產品。我想要的是改爲顯示一個隨機的產品列表,而不是相同的產品。
我發現了顯示產品的功能,但我不確定需要更改orderby
的位置。通過Woocommerce在主頁上隨機顯示產品
以下是將產品寫入頁面的功能。
function hestia_shop_content() {
?>
<div class="hestia-shop-content">
<?php
$hestia_shop_shortcode = get_theme_mod('hestia_shop_shortcode');
if (! empty($hestia_shop_shortcode)) {
echo do_shortcode($hestia_shop_shortcode);
echo '</div>';
return;
}
$hestia_shop_items = get_theme_mod('hestia_shop_items', 4);
$args = array(
'post_type' => 'product',
);
$args['posts_per_page'] = ! empty($hestia_shop_items) ? absint($hestia_shop_items) : 4;
$hestia_shop_categories = get_theme_mod('hestia_shop_categories');
if (sizeof($hestia_shop_categories) >= 1 && ! empty($hestia_shop_categories[0])) {
$args['tax_query'] = array(
array(
'taxonomy' => 'product_cat',
'field' => 'term_id',
'terms' => $hestia_shop_categories,
),
);
}
$hestia_shop_order = get_theme_mod('hestia_shop_order', 'DESC');
if (! empty($hestia_shop_order)) {
$args['order'] = $hestia_shop_order;
}
$loop = new WP_Query($args);
if ($loop->have_posts()) {
$i = 1;
echo '<div class="row">';
while ($loop->have_posts()) {
$loop->the_post();
global $product;
global $post;
?>
<div class="col-ms-6 col-sm-6 col-md-3 shop-item">
<div class="card card-product">
<?php
$thumbnail = hestia_shop_thumbnail(null, 'hestia-shop');
if (empty($thumbnail) && function_exists('wc_placeholder_img')) {
$thumbnail = wc_placeholder_img();
}
if (! empty($thumbnail)) {
?>
<div class="card-image">
<a href="<?php echo esc_url(get_permalink()); ?>"
title="<?php the_title_attribute(); ?>">
<?php echo $thumbnail; ?>
</a>
<div class="ripple-container"></div>
</div>
<?php
}
?>
<div class="content">
<?php
if (function_exists('wc_get_product_category_list')) {
$prod_id = get_the_ID();
$product_categories = wc_get_product_category_list($prod_id);
} else {
$product_categories = $product->get_categories();
}
if (! empty($product_categories)) {
$allowed_html = array(
'a' => array(
'href' => array(),
'rel' => array(),
),
);
echo '<h6 class="category">';
echo wp_kses($product_categories, $allowed_html);
echo '</h6>';
}
?>
<h4 class="card-title">
<a class="shop-item-title-link" href="<?php the_permalink(); ?>"
title="<?php the_title_attribute(); ?>"><?php esc_html(the_title()); ?></a>
</h4>
<?php
if ($post->post_excerpt) {
?>
<div class="card-description"><?php echo apply_filters('woocommerce_short_description', $post->post_excerpt); ?></div>
<?php
}
?>
<div class="footer">
<?php
$product_price = $product->get_price_html();
if (! empty($product_price)) {
echo '<div class="price"><h4>';
echo wp_kses(
$product_price, array(
'span' => array(
'class' => array(),
),
'del' => array(),
)
);
echo '</h4></div>';
}
?>
<div class="stats">
<?php hestia_add_to_cart(); ?>
</div>
</div>
</div>
</div>
</div>
<?php
if ($i % 4 == 0) {
echo '</div><!-- /.row -->';
echo '<div class="row">';
}
$i ++;
}
wp_reset_postdata();
echo '</div>';
}
?>
</div>
<?php
}
這奏效了,非常感謝。 – redleaf