客户想在店鋪頁面上只顯示特色產品。添加特色產品非常簡單(只需使用頁面內容中的 WooCommerce 短代碼),很難刪除 “其他” 產品。這是我做的

WooCommerce 刪除循環在商店頁面

PHP 代碼段:僅刪除產品循環 @WooCommerce 商店頁面

 /**  * @snippet       Remove loop @ WooCommerce shop page only   * @sourcecode    https://businessbloomer.com/?p=17372  * @author        Rodolfo Melogli  * @testedwith    WooCommerce 2.5.5  */  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' );  }  /**  * @snippet       Remove "No products were found..." @ WooCommerce shop page   * @sourcecode    https://businessbloomer.com/?p=17372  * @author        Rodolfo Melogli  * @testedwith    WooCommerce 3.0.4  */  // It's as simple as:  function wc_no_products_found() { 	// do nothing }  

方法 2:通過子主題覆蓋 archive-product.php

1. 在您的子主題文件夾中創建一個文件夾/ woocommerce

2. 將 archive-product.php 文件從 WooCommerce 插件/模板複製到 childtheme / woocommerce

如果您當前的主題沒有/ woocommerce 文件夾,請隨時創建它 🙂

3. 打開 childtheme / woocommerce / archive-product.php 和 註釋掉 “wc_get_template_part(’content’,’product’)” 編輯第 68 行

這個功能正在做的很簡單。它檢查我們是否在商店頁面,如果是,它不執行 wc_get_template_part 函數🙂


可以在哪裏添加此代碼?

您可以將 PHP 代碼片段放置在主題或子主題的 functions.php 文件的底部(如果是 CSS 代碼,請添加到主題的 style.css 文件底部),修改之前建議先備份原始文件,若出現錯誤請先刪除此代碼。


這段代碼是否正常可用?

或者是您有更好的解決方案想要分享?請到薇曉朵 WooCommerce 中文論壇留言告知,我們希望可以幫到更多國內的 WooCommerce 用户也希望您的參與。


需要關於 WooCommerce 的幫助?

請觀看我們提供的免費視頻教程或到薇曉朵 WooCommerce 中文論壇提問,會有專業技術團隊提供相關幫助。