【问题标题】:Woocomerce shop page to show category name & display products within that categoryWoocommerce 商店页面显示类别名称并显示该类别中的产品
【发布时间】:2014-09-23 07:15:38
【问题描述】:

我正在开发一个安装了 Woocommerce 插件的网站。我希望我的商店页面首先显示类别名称,然后显示该类别中的产品,然后显示下一个类别名称及其产品。

已经在 google & here 中搜索过,找到了解决方案,但这不是我想要的。它在每个产品的价格区域后添加了类别名称。我不是专业人士,所以很难解决这个问题。

function wc_category_title_archive_products(){

    $product_cats = wp_get_post_terms( get_the_ID(), 'product_cat' );

    if ( $product_cats && ! is_wp_error ( $product_cats ) ){

        $single_cat = array_shift( $product_cats ); ?>

        <small class="product_category_title"><?php echo $single_cat->name; ?></small>

<?php }
}
add_action( 'woocommerce_after_shop_loop_item', 'wc_category_title_archive_products', 5 );

【问题讨论】:

    标签: wordpress woocommerce


    【解决方案1】:
    <?php    
        $cat_args = array(
            'parent' => '0',
            'taxonomy' => 'product_cat'
        );
        $categories = get_categories( $cat_args );
    
        foreach ($categories as $category) { 
            echo $category->cat_name;
            echo do_shortcode('[product_category category="'.$category->cat_name.'" per_page="12" columns="4" orderby="date" order="DESC"]'); 
        }
    ?> 
    

    【讨论】:

    • 将此代码添加到 /your-child-theme/woocommerce/templates/archive-product.php
    • 我可以确认这是有效的。所以这应该被标记为答案。谢谢@proff。
    • 其实正确的路径应该是/your-child-theme/woocommerce/archive-product.php 请注意我省略了“template”目录。有关更多信息,请阅读此内容。 docs.woocommerce.com/document/template-structure
    • 如果您有兴趣,请阅读此内容docs.woocommerce.com/document/conditional-tags
    【解决方案2】:

    在商店页面的标题上方显示产品类别名称 ==> 检查 Woocomerce 版本 3.5.3 您只需将其复制并粘贴到您的主题函数中即可。php

    function wpa89819_wc_single_product(){
    
        $product_cats = wp_get_post_terms( get_the_ID(), 'product_cat' );
    
        if ( $product_cats && ! is_wp_error ( $product_cats ) ){
    
            $single_cat = array_shift( $product_cats ); ?>
    
            <h2 itemprop="name" class="product_category_title"><span><?php echo $single_cat->name; ?></span></h2>
    
    <?php }
    }
    add_action( 'woocommerce_after_shop_loop_item_title', 'wpa89819_wc_single_product', 5 );
    

    【讨论】:

      【解决方案3】:

      您将不得不为每个类别执行一个新的循环/查询。我们可以通过使用 WordPress 的 get_categories 函数来自动化一些过程。这是一个例子::

      <?php    
          $cat_args = array(
              'parent' => '0',
              'taxonomy' => 'product_cat'
          );
          $categories = get_categories( $cat_args );
      
          foreach ($categories as $category) { ?>      
              <ul class="product-category">
                  <?php
                      $args = array( 'post_type' => 'product', 'posts_per_page' => 1, 'cat' => $category->cat_ID, 'orderby' => 'rand' );
                      $loop = new WP_Query( $args );
                      while ( $loop->have_posts() ) : $loop->the_post(); global $product; ?>
      
                          <!-- Your output -->
      
                  <?php endwhile; ?>
                  <?php wp_reset_query(); ?>
              </ul>
          }
      ?> 
      

      【讨论】:

      • 但不灵活,每次添加新类别都必须更改模板。
      • 您始终可以使用get_categories 函数获取所有类别。之后,您可以对所有这些进行 foreach 循环。那将是灵活的。如果你不明白,我会更新我的答案。
      • 是的,请更新它。我对 php 不是很好。感谢您的辛勤工作。
      • 我已经更新了我的答案,说明您希望如何处理它。在此处发布整个输出将是反生产的。您将需要完成“您的输出”部分。我希望这会有所帮助。
      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2023-03-26
      • 1970-01-01
      • 1970-01-01
      • 2021-09-03
      • 1970-01-01
      • 2020-12-03
      相关资源
      最近更新 更多