如何获取自定义WooCommerce类别模板

时间:2018-06-17 作者:Nimesh

当前我的存档产品。php显示如下。

https://khaleejdev.com/kds/newtornetto/product-category/multilingual-digital-marketing/

其中有15种产品和3个子类别(每个子类别都有简短的描述)第一子类别-6种产品第二子类别-4种产品第三子类别-5种产品

目前只有产品类别和类别的简短描述内拉出。

我想要实现的是

类别简短描述后接第一个子类别标题,简短描述后接it产品(6)第二个子类别标题,简短描述后接it产品(4)第三个子类别标题,简短描述后接it产品(5)目前我在存档产品中有此代码。php

<header class="woocommerce-products-header">
            <?php if ( apply_filters( \'woocommerce_show_page_title\', true ) ) : ?>
                <h1 class="woocommerce-products-header__title page-title"><?php woocommerce_page_title(); ?></h1>
            <?php endif; ?>

            <?php
            /**
             * Hook: woocommerce_archive_description.
             *
             * @hooked woocommerce_taxonomy_archive_description - 10
             * @hooked woocommerce_product_archive_description - 10
             */
            do_action( \'woocommerce_archive_description\' );


            ?>
            <?php 
         global $post;
        $args = array( \'taxonomy\' => \'product_cat\',);
        $terms = wp_get_post_terms($post->ID,\'product_cat\', $args);

            $count = count($terms); 
            if ($count > 0) {

                foreach ($terms as $term) {
                    echo \'<div class="align-center text-justify">\';
                    echo $term->description;
                    echo \'</div>\';

                }

            }

        ?>
        </header>
        <?php
        if ( woocommerce_product_loop() ) {

            /**
             * Hook: woocommerce_before_shop_loop.
             *
             * @hooked wc_print_notices - 10
             * @hooked woocommerce_result_count - 20
             * @hooked woocommerce_catalog_ordering - 30
             */
            do_action( \'woocommerce_before_shop_loop\' );

            woocommerce_product_loop_start();

            if ( wc_get_loop_prop( \'total\' ) ) {
                while ( have_posts() ) {
                    the_post();

                    /**
                     * Hook: woocommerce_shop_loop.
                     *
                     * @hooked WC_Structured_Data::generate_product_data() - 10
                     */
                    do_action( \'woocommerce_shop_loop\' );

                    wc_get_template_part( \'content\', \'product\' );
                }
            }

            woocommerce_product_loop_end();

            /**
             * Hook: woocommerce_after_shop_loop.
             *
             * @hooked woocommerce_pagination - 10
             */
            do_action( \'woocommerce_after_shop_loop\' );
        } else {
            /**
             * Hook: woocommerce_no_products_found.
             *
             * @hooked wc_no_products_found - 10
             */
            do_action( \'woocommerce_no_products_found\' );
        }

        /**
         * Hook: woocommerce_after_main_content.
         *
         * @hooked woocommerce_output_content_wrapper_end - 10 (outputs closing divs for the content)
         */
        do_action( \'woocommerce_after_main_content\' );

The desired format

The steps i explained should be like in image. Please help me to achieve it by customizing current archive-product.php

1 个回复
最合适的回答,由SO网友:LWS-Mo 整理而成

让我们看看这是否有用。根据您的描述,您有/想要以下设置:

-Category 
--Sub-Category-1
---Products of Sub-Category-1
--Sub-Category-2
---Products of Sub-Category-2
--Sub-Category-3
---Products of Sub-Category-3
好的,那么您首先需要获取所有顶级类别。您可以通过以下方式获得它们:

$top_categories_args = array(
    \'taxonomy\' => \'product_cat\', // the taxonomy we want to get terms of
    \'parent\' => 0 // all top level cats with a parent of 0
);
$top_categories = get_terms( $top_categories_args );
通过foreach循环迭代所有顶级CAT:

foreach ($top_categories as $top_category) {

    $top_id = $top_category->term_id; // get term ID
    $top_slug = $top_category->slug; // get term slug
    $top_name = $top_category->name; // get term title
    $top_desc = $top_category->description; // get term description

    echo \'<div class="\'.$top_slug.\'">\';

    echo \'<h2>\'.$top_name.\'</h2>\';

    if ($top_desc) {
        echo \'<p>\'.$top_desc.\'</p>\';
    }

    // here we now need to get all the sub-categories

    echo \'</div><!-- END top categories container -->\';

}
现在我们需要一个类似的新get_terms 查询子类别:(应替换”//此处我们现在需要获取所有子类别)

// here we get all the sub categories of the current cat

$sub_categories_args = array(
    \'taxonomy\' => \'product_cat\',
    \'parent\' => $top_id // we use the top_id from before to get only sub-cats
);
$sub_categories = get_terms( $sub_categories_args );

foreach ($sub_categories as $sub_category) {

    $sub_id = $sub_category->term_id;
    $sub_slug = $sub_category->slug;
    $sub_name = $sub_category->name;
    $sub_desc = $sub_category->description;

    echo \'<div class="\'.$top_slug.\'-\'.$sub_slug.\'">\';

    echo \'<h3>\'.$sub_name.\'</h3>\';

    if ($sub_desc) {
        echo \'<p>\'.$sub_desc.\'</p>\';
    }

    // here we now need to get all the products inside this sub-categories

    echo \'</div><!-- END sub categories container -->\';

}
现在,在这个子类别代码中,您现在可以通过结合tax\\u查询的查询来获取产品。(应替换“/”,我们现在需要获取此子类别中的所有产品)

$products_args = array(
    \'post_type\'     => \'product\', 
    \'tax_query\'     => array( 
        array(
            \'taxonomy\' => \'product_cat\',
            \'field\'    => \'term_id\', // we look for the ID, you could also use slug
            \'terms\'    => $sub_id, // get only products with the sub-cat ID
        ),
    ),
);
$products = new WP_Query( $products_args );

if ( $products->have_posts() ) { // only start if we hace some products

    // START some normal woocommerce loop

    woocommerce_product_loop_start();

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

        wc_get_template_part( \'content\', \'product\' );

    endwhile; // end of the loop.

    woocommerce_product_loop_end();

    // END the normal woocommerce loop

    // Restore original post data, maybe not needed here (in a plugin it might be necessary)
    wp_reset_postdata();

} else { // if we have no products, show the default woocommerce no-product loop

    // no posts found
    wc_get_template( \'loop/no-products-found.php\' );

}//END if $products
我在这里创建了一个要点,以及完整的产品档案。php和更多详细信息:product-archive.php

如果您有一些不想显示的术语,可以使用excludeexclude_tree, 禁用它们。有关更多选项,请查看codex页面here.

// first we get all top-level categories
$top_categories_args = array(
    \'taxonomy\' => \'product_cat\', // the taxonomy we want to get terms of
    \'parent\' => 0, // all top level cats with a parent of 0
    \'hide_empty\' => true,
    \'exclude\' => \'11,23,99\', // Array or comma/space-separated string of term ids to exclude.
    \'exclude_tree\' => \'2,5,12\' // Array or comma/space-separated string of term ids to exclude along with all of their descendant terms.
);
您还可以使用WooCommerce的默认拖放功能更改条款的顺序。只需在后端拖放术语。

结束

相关推荐

WP_DROPDOWN_CATEGORIES-如何在Widget中保存?

我想用wp_dropdown_categories 在自定义小部件中。所有内容都显示得很好,但由于某些原因,无法正确保存。这是form() 和update() 小部件的功能-我做错什么了吗?public function form( $instance ) { /* Set up some default widget settings. */ $defaults = array( \'title\' => \'Classes by Category\' );&#x