Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/visual-studio-2010/4.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Php 仅在Woocommerce商店页面中显示特色产品_Php_Wordpress_Woocommerce_Product - Fatal编程技术网

Php 仅在Woocommerce商店页面中显示特色产品

Php 仅在Woocommerce商店页面中显示特色产品,php,wordpress,woocommerce,product,Php,Wordpress,Woocommerce,Product,我想在WooCommerce的默认商店页面中仅显示特色产品,仅此而已。。。 有一种解决方案可以在WooCommerce商店模板中仅显示特色产品?首先,您必须将archive-product.php模板覆盖到主题文件中 然后添加以下代码以在shop pahe中显示特色产品 <?php $meta_query = WC()->query->get_meta_query();

我想在WooCommerce的默认商店页面中仅显示特色产品,仅此而已。。。
有一种解决方案可以在WooCommerce商店模板中仅显示特色产品?

首先,您必须将archive-product.php模板覆盖到主题文件中

然后添加以下代码以在shop pahe中显示特色产品

    <?php 
                          $meta_query  = WC()->query->get_meta_query();
                          $tax_query   = WC()->query->get_tax_query();
                          $tax_query[] = array(
                              'taxonomy' => 'product_visibility',
                              'field'    => 'name',
                              'terms'    => 'featured',
                              'operator' => 'IN',
                          );

                          $args = array(
                              'post_type'           => 'product',
                              'post_status'         => 'publish',                          
                              'posts_per_page'      => '5',
                              'orderby'             => 'DESC',                          
                              'meta_query'          => $meta_query,
                              'tax_query'           => $tax_query,
                          );
                            $loop = new WP_Query( $args );

                            if ( $loop->have_posts() ) {
                                while ( $loop->have_posts() ) : $loop->the_post();
                                  $id = $product->get_id();
                                  $image_sale = wp_get_attachment_image_src( get_post_thumbnail_id( $loop->post->ID ), 'single-post-thumbnail' );
                                  $product_url = get_permalink($id);
                                  $product = wc_get_product($id);
                                  $product_title = $product->get_title();                              
                                  $sale_price =  $product->get_price();

                                  ?>

                     <div class="item">
                              <div class="product-box">
                                 <div class="product-img">
                              <a href="<?php echo $product_url;?>" title="" ><img src="<?php  echo $image_sale[0]; ?>" data-id="<?php echo $id; ?>"></a>                                    
                                 </div>
                                 <div class="product-content">
                                    <h5><?php echo $product_title;?></h5> 
                                    <P>$<?php echo $sale_price;?>,00</P>
                                 </div>
                              </div>
                         </div>


<?php 
            endwhile;
        } else {
            echo __( 'No products found' );
        }
        wp_reset_postdata();
    ?>

美元,00


您应该在
woocommerce\u product\u query\u tax\u query
过滤器钩子中使用此自定义功能,该钩子将仅在商店中显示特色产品(但不在其他存档页面中):


代码进入活动子主题(或活动主题)的function.php文件。已测试并运行。

在functions.php中使用以下代码在商店页面中显示特色产品

<?php 
add_action( 'woocommerce_product_query', 'ss_custom_product_query' );

function ss_custom_product_query( $q ){
    $meta_query = $q->get( 'meta_query' );
    if ( is_shop() ) {
        $meta_query[] = array(
            'key'   => '_featured',
            'value' => 'yes'
        );
    }
    $q->set( 'meta_query', $meta_query );
}
?>


能否请您更新您的答案,包括页码和每页10种产品的限制?@WooQA您最好提出一个新问题,因为这是不同的。这里我刚刚回答了你最初的问题。
<?php 
add_action( 'woocommerce_product_query', 'ss_custom_product_query' );

function ss_custom_product_query( $q ){
    $meta_query = $q->get( 'meta_query' );
    if ( is_shop() ) {
        $meta_query[] = array(
            'key'   => '_featured',
            'value' => 'yes'
        );
    }
    $q->set( 'meta_query', $meta_query );
}
?>