繁体   English   中英

WooCommerce产品按类别订购

[英]WooCommerce products ordered by categories

我有WooCommerce属性存档页面,需要显示按类别排序的产品。 像这样:

A类

  • 产品X
  • 产品Y
  • 产品Z

B类

  • 产品A
  • 产品B
  • 产品C

等等

现在,我当前的自定义循环的结果是:

A类

  • 产品A
  • 产品B
  • 产品C
  • 产品X
  • 产品Y
  • 产品Z

B类

  • 产品A
  • 产品B
  • 产品C
  • 产品X
  • 产品Y
  • 产品Z

等等

因此,它基本上显示了每个类别中属性项内的所有产品,即使该产品不在此类别中也是如此。 我认为我正在忽略某些东西,但我不知道该怎么办?

<?php
/**
 * Show products sorted by category
 */

$taxonomy       = 'product_cat';
$orderby        = 'menu_order';
$order          = 'ASC';
$show_count     = 1; // 1 for yes, 0 for no
$pad_counts     = 0; // 1 for yes, 0 for no
$hierarchical   = 1; // 1 for yes, 0 for no
$title          = '';
$empty          = 1; // 1 for hide, 0 for show

$args = array(
    'taxonomy'      => $taxonomy,
    'orderby'       => $orderby,
    'order'         => $order,
    'show_count'    => $show_count,
    'pad_counts'    => $pad_counts,
    'hierarchical'  => $hierarchical,
    'title_li'      => $title,
    'hide_empty'    => $empty,
);

$all_categories = get_categories( $args );

foreach ($all_categories as $cat) :

    $term_link = get_term_link( $cat );

    if( $cat->category_parent == 0 ) :

        $category_id    = $cat->term_id;
        $thumbnail_id   = get_term_meta( $cat->term_id, 'thumbnail_id', true );
        $image          = wp_get_attachment_url( $thumbnail_id );

        echo '<div id="' . $cat->slug . '" class="category ' . $cat->slug . '"><h2 class="category-title"><a href="'. esc_url( $term_link ) .'">'.$cat->name.'</a></h2>';

            $loop = new WP_Query( $args );

            while ( have_posts() ) : the_post();

                global $product;

                wc_get_template_part( 'content', 'product' );

            endwhile; // end of the loop. ?>

            </div><!-- end .category -->

            <?php

            wp_reset_query();

        endif;

    endforeach;

    wp_reset_query();

    ?>

添加以下代码以获取类别明智的产品

    <?php
/**
 * Show products sorted by category
 */

$taxonomy       = 'product_cat';
$orderby        = 'menu_order';
$order          = 'ASC';
$show_count     = 1; // 1 for yes, 0 for no
$pad_counts     = 0; // 1 for yes, 0 for no
$hierarchical   = 1; // 1 for yes, 0 for no
$title          = '';
$empty          = 1; // 1 for hide, 0 for show


$args = array(
    'taxonomy'      => $taxonomy,
    'orderby'       => $orderby,
    'order'         => $order,
    'show_count'    => $show_count,
    'pad_counts'    => $pad_counts,
    'hierarchical'  => $hierarchical,
    'title_li'      => $title,
    'hide_empty'    => $empty,
);




$all_categories = get_categories( $args );

foreach ($all_categories as $cat) :

    $term_link = get_term_link( $cat );

    if( $cat->category_parent == 0 ) :

        $category_id    = $cat->term_id;
        $thumbnail_id   = get_term_meta( $cat->term_id, 'thumbnail_id', true );
        $image          = wp_get_attachment_url( $thumbnail_id );

        echo '<div id="' . $cat->slug . '" class="category ' . $cat->slug . '"><h2 class="category-title"><a href="'. esc_url( $term_link ) .'">'.$cat->name.'</a></h2>';

            $product_args = array(
                'type'          => 'product',
                'post_status'   => 'publish',
                'orderby'       => 'name',
                'order'         => 'DESC',
                'pad_counts'    => false,
                'hierarchical'  => 1,
                'hide_empty'    => 0,
                'tax_query'                => array(
                    array(
                        'taxonomy' => 'product_cat',
                        'field' => 'id',
                        'terms' => $cat->term_id
                    )
                ),
            );

            $loop = new WP_Query( $product_args );

            while ( have_posts() ) : the_post();

                global $product;

                wc_get_template_part( 'content', 'product' );

            endwhile; // end of the loop. ?>

            </div><!-- end .category -->

            <?php

            wp_reset_query();

        endif;

    endforeach;

    wp_reset_query();

 ?>

get_categories()WP_Query()的参数不同。

关于明智地获取产品类别的争论。

$args_for_product = array(
            'tax_query' => array(array('taxonomy' =>$taxonomy,'field' => 'slug','terms' =>$cat->slug)),
            'orderby'       => $orderby,
            'order'         => $order,
);

使用tax_query传递产品查询中的类别。 show_count,pad_counts,hierarchical,title_li,hide_empty其他参数( 例如show_count,pad_counts,hierarchical,title_li,hide_empty不通过WP_Query传递 )。 将上面的代码放在类别循环中

同时在while循环have_post()更改为$loop->have_post() ,将the_post()更改$loop->the_post()

完整的代码

<?php
/**
 * Show products sorted by category
 */

$taxonomy       = 'product_cat';
$orderby        = 'menu_order';
$order          = 'ASC';
$show_count     = 1; // 1 for yes, 0 for no
$pad_counts     = 0; // 1 for yes, 0 for no
$hierarchical   = 1; // 1 for yes, 0 for no
$title          = '';
$empty          = 1; // 1 for hide, 0 for show

$args = array(
    'taxonomy'      => $taxonomy,
    'orderby'       => $orderby,
    'order'         => $order,
    'show_count'    => $show_count,
    'pad_counts'    => $pad_counts,
    'hierarchical'  => $hierarchical,
    'title_li'      => $title,
    'hide_empty'    => $empty,
);

$all_categories = get_categories( $args );

foreach ($all_categories as $cat) :

    $term_link = get_term_link( $cat );

    if( $cat->category_parent == 0 ) :

        $category_id    = $cat->term_id;
        $thumbnail_id   = get_term_meta( $cat->term_id, 'thumbnail_id', true );
        $image          = wp_get_attachment_url( $thumbnail_id );

        echo '<div id="' . $cat->slug . '" class="category ' . $cat->slug . '"><h2 class="category-title"><a href="'. esc_url( $term_link ) .'">'.$cat->name.'</a></h2>';

        $args_for_product = array('type'=> 'product',
            'post_status'   => 'publish',
            'tax_query' => array(array('taxonomy' =>$taxonomy,'field' => 'slug','terms' =>$cat->slug)),
            'orderby'       => $orderby,
            'order'         => $order,
        );
            $loop = new WP_Query( $args_for_product );

            while ( $loop->have_posts() ) : $loop->the_post();

                global $product;

                wc_get_template_part( 'content', 'product' );

            endwhile; // end of the loop. ?>

            </div><!-- end .category -->

            <?php

            wp_reset_query();

        endif;

    endforeach;

    wp_reset_query();

    ?>

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM