在 Woocommerce 商店页面中仅显示特色产品 [英] Show only featured products in Woocommerce shop page

查看:60
本文介绍了在 Woocommerce 商店页面中仅显示特色产品的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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

I want to display in default shop page from WooCommerce only featured products, nothing more... It's there a solution to display in WooCommerce shop template only the featured products?

推荐答案

你应该使用这个挂在 woocommerce_product_query_tax_query 过滤钩子中的自定义函数,它只会显示商店中的特色产品(而不是其他档案中的)页):

You should use this custom function hooked in woocommerce_product_query_tax_query filter hook, that will display only featured product in shop (but not in other archives pages):

// Display featured products in shop pages
add_filter( 'woocommerce_product_query_tax_query', 'custom_product_query_tax_query', 10, 2 );
function custom_product_query_tax_query( $tax_query, $query ) {
    if( is_admin() ) return $tax_query;

    if ( is_shop() ) {
        $tax_query[] = array(
            'taxonomy' => 'product_visibility',
            'field'    => 'name',
            'terms'    => 'featured'
        );
    }

    return $tax_query;
}

代码位于活动子主题(或活动主题)的 function.php 文件中.经过测试并有效.

这篇关于在 Woocommerce 商店页面中仅显示特色产品的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆