使用Woocommerce随机化主页上显示的产品

时间:2017-10-28 18:02:35

标签: php wordpress woocommerce

目前,我的主题在主页的某个部分显示最近添加的WooCommerce产品。我想要的是显示一个随机的产品列表而不是相同的产品列表 我找到了显示产品的功能,但我不确定在哪里需要更改orderby

以下是将产品写入页面的功能。

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

}

1 个答案:

答案 0 :(得分:1)

在您的查询中使用rand命令,如下所示

  $args = array(
    'post_type' => 'product',
    'orderby'=> 'rand'
);