根据类别显示随机产品

时间:2020-08-21 14:23:16

标签: php html wordpress woocommerce

我在某个产品的单个产品页面中。 我想在此产品之后展示同一类别

的其他4个随机产品

我所拥有的就是这个:(PHP)

import java.util.*;
public class Main {
    public static void bubbleSort(int[] arr){
        int i,j,t,n;
        n=arr.length;
        for (i=0;i<(n-1);++i){
            for (j=1;j<(n-i-1);++j){
                if(arr[i]>arr[i=1]){
                t=arr[i];
                arr[i]=arr[i+1];
                arr[i+1]=t;
                }
            }
        }
        printSortedArray(arr);
    }
    public static void printSortedArray(int[] arr){
        System.out.print("{");
        for (int j : arr) System.out.print(j + ",");
        System.out.print("}");

    }
    public static void main(String[] args) {
        int n;
        Scanner a = new Scanner(System.in);
        System.out.println("Enter a range for array");
        n=a.nextInt();
        int [] b=new int[n];
        for (int i=0;i<n;++i)
            b[i]=a.nextInt();
        bubbleSort(b);

    }
}

有人可以帮助我吗?谢谢

1 个答案:

答案 0 :(得分:1)

您可以尝试这种方式。我检查了它的作用:

global $product;
    $terms = get_the_terms( $product->get_id(), 'product_cat' );
    $first_term = array_shift( $terms );

    $args = array(
        'posts_per_page'   => 4,
        'orderby'          => 'rand',
        'post_type'        => 'product',
        'tax_query' => array(
            array(
                'taxonomy' => 'product_cat',
                'field'    => 'slug',
                'terms'    => array( $first_term->slug )
            )
        ),
        'post__not_in' => array( $product->get_id() )
    );

    $random_products = get_posts( $args );

    foreach ( $random_products as $post ) : setup_postdata( $post ); ?>
        <li>
            <a href="<?php the_permalink(); ?>">
                <?php the_title(); ?></a>
        </li>
    <?php endforeach;
    wp_reset_postdata();
    ?>