亚洲在线久爱草,狠狠天天香蕉网,天天搞日日干久草,伊人亚洲日本欧美

為了賬號安全,請及時綁定郵箱和手機立即綁定
已解決430363個問題,去搜搜看,總會有你想問的

根據類別顯示隨機產品

根據類別顯示隨機產品

PHP
慕標5832272 2023-10-21 19:58:14
我在產品的單個產品頁面中。我想在該產品之后展示 4 個其他相同類別的隨機產品我所擁有的就是這個:(PHP)<?php        global $product;        $terms = get_the_terms( $product->get_id(), 'product_cat' );        var_dump($terms);    $args = array(        'posts_per_page'   => 4,        'orderby'          => 'rand',                'post_type'        => 'product' );     $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();?>誰能幫我?謝謝
查看完整描述

1 回答

?
青春有我

TA貢獻1784條經驗 獲得超8個贊

你可以試試這個方法。我檢查了它是否有效:


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

    ?>


查看完整回答
反對 回復 2023-10-21
  • 1 回答
  • 0 關注
  • 132 瀏覽

添加回答

舉報

0/150
提交
取消
微信客服

購課補貼
聯系客服咨詢優惠詳情

幫助反饋 APP下載

慕課網APP
您的移動學習伙伴

公眾號

掃描二維碼
關注慕課網微信公眾號