我使用以下代码来隐藏 Woo 商店页面上的产品循环:
add_action( 'pre_get_posts', 'bbloomer_remove_products_from_shop_page' );
function bbloomer_remove_products_from_shop_page( $q ) {
if ( ! $q->is_main_query() ) return;
if ( ! $q->is_post_type_archive() ) return;
if ( ! is_admin() && is_shop() ) {
$q->set( 'tax_query', array(array(
'taxonomy' => 'product_cat',
'field' => 'slug',
'terms' => array( 'null' ),
'operator' => 'IN'
)));
}
remove_action( 'pre_get_posts', 'bbloomer_remove_products_from_shop_page' );
}
function wc_no_products_found() {
if ( is_shop() ) {
echo '<style>p.woocommerce-info{display:none}</style';
}
}
它有效。它会删除所有产品和“Woo no products found”错误消息。
问题如下:
因为上面的代码删除了没有找到产品的错误消息。如果有人在我的类别之一中搜索不存在的内容,则结果搜索结果页面也会是空白的。
简单来说,我只想从商店页面删除“找不到产品”错误消息。我已经删除了所有产品。并且在(不成功的)搜索结果页面上仍然显示错误消息。
提前感谢您的帮助。
add_action( 'woocommerce_no_products_found', function(){
if(is_shop()) {
remove_action( 'woocommerce_no_products_found', 'wc_no_products_found', 10 );
}
}, 9 );
希望这段代码对你有帮助
如果 archive-product.php 没有更改任何内容,请添加:
add_theme_support('woocommerce');
到你的functions.php