【问题标题】:Display Subcategory WooCommerce products with titles显示带有标题的子类别 WooCommerce 产品
【发布时间】:2018-11-09 12:53:58
【问题描述】:

我将 woocommerce 产品的显示更改为在商店循环中显示为表格,而不是列元素。当我转到“商店”页面时,它会在列表中显示所有产品。我无法以有组织的方式展示产品,将产品分成相应的子类别。提前致谢!

【问题讨论】:

  • 请阅读how to ask并相应地编辑您的问题

标签: php wordpress woocommerce


【解决方案1】:
/**
* @snippet       WooCommerce Show Product Subcategories
* @compatible    WooCommerce 3.4
*/

add_action( 
'woocommerce_after_shop_loop_item_title','bbloomer_show_all_subcats', 2 );

function bbloomer_show_all_subcats() {

// Create array with product categories that belong to current product

$cats = get_the_terms( $post->ID, 'product_cat' );

if ( ! empty( $cats ) ) {

// Loop through the product categories...

    foreach ( $cats as $term ) {

                    // If parent cat ID = 116 echo subcat name...
        if( $term->parent == 116 ) { echo $term->name; }

    }

    }

  }

【讨论】:

  • 太棒了,谢谢!我最终弄清楚了。我能够在循环中提取所有类别和子类别的
      链接到相应页面。从那里我插入循环以显示当前类别的产品。
【解决方案2】:
$args = array(
    'taxonomy' => 'product_cat',
    'hide_empty' => false,
    'parent'   => 0
);

$product_cat = get_terms( $args );

foreach ($product_cat as $parent_product_cat)
{
    if ($parent_product_cat->name != 'Company'){
    ?>

    <ul>
        <li><h2><a href='<?= get_term_link($parent_product_cat->term_id) ?>'><?= $parent_product_cat->name ?></a></h2>
            <hr align='left' width='50%'>
            <ul>

                <?php
                $child_args = array(
                    'taxonomy' => 'product_cat',
                    'hide_empty' => false,
                    'parent'   => $parent_product_cat->term_id
                );

                $child_product_cats = get_terms( $child_args );
                foreach ($child_product_cats as $child_product_cat)
                { ?>
                    <li style='padding-left: 2%;'>
                        <h3><a href='<?= get_term_link($child_product_cat->term_id) ?>'><?= $child_product_cat->name?></a></h3>
                    </li>
                    <div style='margin-left: -8%;'>
                        <?php
                        echo do_shortcode("[products category='$child_product_cat->term_id']");
                        ?>
                    </div>
                    <?php
                }
                ?>

            </ul>
        </li>
    </ul>

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2017-03-20
    • 2018-06-14
    • 1970-01-01
    • 2020-09-17
    • 2018-05-17
    • 1970-01-01
    • 1970-01-01
    • 2016-06-07
    相关资源
    最近更新 更多