在WooCommerce商店页面中隐藏无缩略图的产品 [英] Hiding products without thumbnail in WooCommerce shop page
问题描述
我使用进口商将数千种产品进口到商店.尽管我要出售产品之前必须先插入产品图片和说明.
I use importer which imports thousands of products to the shop. Although I must insert picture and description for the product before I want to sell the item.
如果未分配缩略图,我想将所有产品都隐藏在商店中.这样,只有在设置缩略图后,新产品才会出现在商店中.
I would like to hide product from the store at all IF there is no thumbnail image assigned. This way new products appear to the shop only after I have set the thumbnail image.
我在header.php中尝试了此操作,但是没有用:
I tried this in header.php, but did not work:
<?php if($_product->getImage() && $_product->getImage() != 'no_selection'){
?>
<style>
/* Css to hide Featured image Div */
</style>
<?php
}?>
这给了我错误:
This gave me error:
致命错误:在非对象上调用成员函数getImage()
Fatal error: Call to a member function getImage() on a non-object
如果没有放置缩略图的图片,是否有任何快速方法可以从商店"页面隐藏WordPress WooCommerce产品?
Is there any quick way to hide WordPress WooCommerce products from the Shop page, if they do not have a thumbnail picture placed?
谢谢
推荐答案
您可以使用挂钩在 woocommerce_product_query
操作中的自定义函数,该挂钩将更改商店查询并不会通过以下方式在商店页面上显示没有图像的产品:
You can use a custom function hooked in woocommerce_product_query
action, hook that will change the shop query and will not display on shop page products without an image, this way:
add_action( 'woocommerce_product_query', 'custom_pre_get_posts_query' );
function custom_pre_get_posts_query( $query ) {
$query->set( 'meta_query', array( array(
'key' => '_thumbnail_id',
'value' => '0',
'compare' => '>'
)));
}
代码会出现在您活动的子主题(或主题)的function.php文件或任何插件文件中.
此代码已经过测试,可在WooCommerce 2.6.x和3.0+上运行
This code is tested and works on WooCommerce 2.6.x and 3.0+
这篇关于在WooCommerce商店页面中隐藏无缩略图的产品的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!