为了账号安全,请及时绑定邮箱和手机立即绑定

Woocommerce 在商店和类别上方显示特色和隐藏产品

Woocommerce 在商店和类别上方显示特色和隐藏产品

PHP
SMILET 2022-12-03 10:47:12
我需要在商店和类别页面上的循环上方显示特色产品。我能够做到这一点,但我还需要在默认循环中隐藏这些产品。我想通过产品可见性设置来解决这个问题,但产品总是在两个循环中显示或隐藏。理想情况下,我想将产品设置为特色和隐藏,并将我的自定义循环设置为忽略可见性设置。我就是想不通。我目前正在使用此代码:function featured_products_beforeloop() {    $args = array(        'post_type' => 'product',        'posts_per_page' => 12,        'tax_query' => array(            array(                'field'    => 'name',                'terms'    => 'featured',            ),        ),    );    $loop = new WP_Query( $args ); ?>    <ul class="products columns-<?php echo esc_attr( wc_get_loop_prop( 'columns' ) ); ?>">        <?php while ( $loop->have_posts() ) :         global $product; ?>            <li <?php wc_product_class( '', $product ); ?>>              <?php  if ( has_post_thumbnail( $loop->post->ID ) )                 echo get_the_post_thumbnail( $loop->post->ID, 'shop_catalog' );             else                 echo '<img src="' . woocommerce_placeholder_img_src() . '" alt="' . $product->get_title() . '" class="attachment-shop_catalog size-shop_catalog wp-post-image" width="300px" height="300px" />'; ?>            <h3><?php the_title(); ?></h3>            <?php if ( $price_html = $product->get_price_html() ) : ?>            <span class="price"><?php echo $price_html; ?></span>            <?php endif;            woocommerce_template_loop_add_to_cart( $loop->post, $product ); ?>            </li>        <?php     endwhile; ?></ul> <?php    wp_reset_postdata();    } add_action( 'woocommerce_before_shop_loop', 'featured_products_beforeloop', 10 );
查看完整描述

1 回答

?
UYOU

TA贡献1878条经验 获得超4个赞

你有没有尝试过:


$args = array(

    'post_type' => 'product',

    'posts_per_page' => 12,


    'tax_query' => array(

        array(

            'taxonomy' => 'product_visibility',

            'field'    => 'name',

            'terms'    => 'featured',

        ),

        // allow both hidden and not hidden featured items

        array(

            'relation' => 'OR',

            array(

                'taxonomy' => 'product_visibility',

                'field'    => 'name',

                'terms'    => 'hidden',

                'operator' => 'NOT IN',

            ),

            array(

                'taxonomy' => 'product_visibility',

                'field'    => 'name',

                'terms'    => 'hidden',

                'operator' => '=',

            ),

        )

    ),

);


查看完整回答
反对 回复 2022-12-03
  • 1 回答
  • 0 关注
  • 128 浏览

添加回答

举报

0/150
提交
取消
微信客服

购课补贴
联系客服咨询优惠详情

帮助反馈 APP下载

慕课网APP
您的移动学习伙伴

公众号

扫描二维码
关注慕课网微信公众号