【问题标题】:Only display Woocommerce child product categories仅显示 Woocommerce 子产品类别
【发布时间】:2018-05-17 20:17:20
【问题描述】:

我目前正在使用以下代码来获取 WooCommerce 产品类别:

<?php
    $orderby = 'name';
    $order = 'asc';
    $hide_empty = false ;
    $cat_args = array(
        'orderby'    => $orderby,
        'order'      => $order,
        'hide_empty' => $hide_empty,
    );

    $product_categories = get_terms( 'product_cat', $cat_args );

    if( !empty($product_categories) ){
        echo '<ul>';
        foreach ($product_categories as $key => $category) {
            echo '<li>';
            echo '<a href="'.get_term_link($category).'" >';
            echo $category->name;
            echo '</a>';
            echo '<li>';
        }
        echo '</ul>';
    }
?>

目前显示所有类别,但我希望仅显示子类别。

例如,如果您在类别 1 页面上,它应该只显示该类别中的所有子项。

我在这里查看了很多示例,但找不到适合我需要的东西。

【问题讨论】:

  • 你能分享一些示例查询结果吗?
  • 您不会将当前类别作为参数发送,您的 SQL 查询也必须使用此参数过滤结果。如果您可以分享您的 SQL 查询,我们可以观察它。

标签: php wordpress woocommerce categories custom-taxonomy


【解决方案1】:

这是一种仅获取按名称 ASC 排序的产品子类别链表的方法:

// The product category taxonomy
$taxonomy = 'product_cat';

// Get the parent categories IDs
$parent_cat_ids = get_terms( $taxonomy, array(
    'parent'     => 0,
    'hide_empty' => false,
    'fields'     => 'ids'
) );

// Get only "child" WP_Term Product categories
$subcategories = get_terms( $taxonomy, array(
    'exclude'     => $parent_cat_ids,
    'orderby'    => 'name',
    'order'      => 'asc',
    'hide_empty' => false,
) );

if( ! empty( $subcategories ) ){
    echo '<ul>';
    foreach ($subcategories as $subcategory) {
        echo '<li>
            <a href="'. get_term_link($subcategory) .'" >' . $subcategory->name.'</a>
        </li>';
    }
    echo '</ul>';
}

代码已经过测试并且可以工作

【讨论】:

    【解决方案2】:

    您可以将其添加到 woocommerce/archive-product.php 文件中

    $queried_object = get_queried_object();
    $parent = $queried_object->term_id;
    $categories = get_term_children( $parent, 'product_cat' ); 
    if ( $categories && ! is_wp_error( $category ) ) : 
    
    echo '<ul>';
    
    foreach($categories as $category) :
    
    $term = get_term( $category, 'product_cat' );
    echo '<li>';
    echo '<a href="'.get_term_link($term).'" >';
    echo $term->name;
    echo '</a>';
    echo '</li>';
    
    endforeach;
    
    echo '</ul>';
    
    endif;
    

    这仅适用于您的档案。以及有孩子的类别。

    它还会输出大子类别。

    希望这会有所帮助。

    【讨论】:

      【解决方案3】:

      在主题的functions.php文件中添加下面的代码。

      add_filter('get_child_category', 'get_child_cat', 10, 1);
      function get_child_cat($term_id = 0){
          global $wpdb;
          $result = array('status' => 'fail');
          if($term_id != 0){
              $result = $wpdb->get_results( "select ".$wpdb->prefix."terms.term_id, ".$wpdb->prefix."terms.name, ".$wpdb->prefix."terms.slug, ".$wpdb->prefix."terms.term_group, ".$wpdb->prefix."term_taxonomy.term_taxonomy_id, ".$wpdb->prefix."term_taxonomy.taxonomy, ".$wpdb->prefix."term_taxonomy.description, ".$wpdb->prefix."term_taxonomy.parent, ".$wpdb->prefix."term_taxonomy.count from ".$wpdb->prefix."terms left join ".$wpdb->prefix."term_taxonomy on ".$wpdb->prefix."term_taxonomy.term_id = ".$wpdb->prefix."terms.term_id where ".$wpdb->prefix."term_taxonomy.parent = $term_id" );
          }
          return $result;
      }
      

      使用以下代码获取子类别的数组输出。

      $cat = apply_filters( 'get_child_category', $term_id ); // where $term_id is your parent category id.
      

      注意:此代码仅用于单级术语。 根据您的要求更改代码。

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2016-06-07
        • 2022-07-08
        • 1970-01-01
        • 1970-01-01
        • 2021-09-03
        相关资源
        最近更新 更多