Warning: file_get_contents(/data/phpspider/zhask/data//catemap/1/wordpress/13.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
Wordpress 按类别展示产品_Wordpress_Woocommerce - Fatal编程技术网

Wordpress 按类别展示产品

Wordpress 按类别展示产品,wordpress,woocommerce,Wordpress,Woocommerce,我有以下代码在商店页面中显示类别下的产品: add_action('woocommerce_before_shop_loop', 'seperate_collection', 10); function seperate_collection(){ // men's collections if ( is_shop() ){ $cat_args = array( 'taxonomy' => 'product_cat',

我有以下代码在商店页面中显示类别下的产品:

add_action('woocommerce_before_shop_loop', 'seperate_collection', 10);
function seperate_collection(){
    // men's collections
    if ( is_shop() ){
        $cat_args = array(
            'taxonomy' => 'product_cat',
            'parent' => 42 // for-him category id.
        );
        $categories = get_categories( $cat_args );
        foreach ($categories as $category) { 
            echo '<h3>' . $category->cat_name . '</h3>';
            echo do_shortcode('[products category="' . $category->slug . '" columns="4" orderby="date" order="DESC"]'); 
        }

    }

}
但它最终删除了所有内容,包括分类下的产品


提前感谢。

为什么不从商店页面模板中删除主循环?@disin谢谢。我尝试删除循环,但我仍然在底部得到页码,就好像有产品一样。您应该能够删除模板上的所有分页和主循环。您可能需要发布更多信息。
add_action( 'pre_get_posts', 'remove_products_from_shop_page' );

function 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( 'post__in', array(0) );
   }
   remove_action( 'pre_get_posts', 'remove_products_from_shop_page' );
}