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

我需要在商店和类别页面上的循环上方显示特色产品。我能够做到这一点,但我还需要在默认循环中隐藏这些产品。我想通过产品可见性设置来解决这个问题,但产品总是在两个循环中显示或隐藏。


理想情况下,我想将产品设置为特色和隐藏,并将我的自定义循环设置为忽略可见性设置。我就是想不通。


我目前正在使用此代码:


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


SMILET
浏览 216回答 1
1回答

UYOU

你有没有尝试过:$args = array(&nbsp; &nbsp; 'post_type' => 'product',&nbsp; &nbsp; 'posts_per_page' => 12,&nbsp; &nbsp; 'tax_query' => array(&nbsp; &nbsp; &nbsp; &nbsp; array(&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; 'taxonomy' => 'product_visibility',&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; 'field'&nbsp; &nbsp; => 'name',&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; 'terms'&nbsp; &nbsp; => 'featured',&nbsp; &nbsp; &nbsp; &nbsp; ),&nbsp; &nbsp; &nbsp; &nbsp; // allow both hidden and not hidden featured items&nbsp; &nbsp; &nbsp; &nbsp; array(&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; 'relation' => 'OR',&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; array(&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; 'taxonomy' => 'product_visibility',&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; 'field'&nbsp; &nbsp; => 'name',&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; 'terms'&nbsp; &nbsp; => 'hidden',&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; 'operator' => 'NOT IN',&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; ),&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; array(&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; 'taxonomy' => 'product_visibility',&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; 'field'&nbsp; &nbsp; => 'name',&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; 'terms'&nbsp; &nbsp; => 'hidden',&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; 'operator' => '=',&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; ),&nbsp; &nbsp; &nbsp; &nbsp; )&nbsp; &nbsp; ),);
打开App,查看更多内容
随时随地看视频慕课网APP