Php 仅显示子产品类别

Php 仅显示子产品类别,php,wordpress,woocommerce,categories,custom-taxonomy,Php,Wordpress,Woocommerce,Categories,Custom Taxonomy,我目前正在使用以下代码获取WooCommerce产品类别: <?php $orderby = 'name'; $order = 'asc'; $hide_empty = false ; $cat_args = array( 'orderby' => $orderby, 'order' => $order, 'hide_empty' => $hide_empty, );

我目前正在使用以下代码获取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”页面上,则该页面应仅显示该类别中的所有子级


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

在theme的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.
注:此代码仅用于单级术语。
根据需要更改代码。

您可以将其添加到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;
$queryed_object=get_queryed_object();
$parent=$queryed\u object->term\u id;
$categories=get_term_children($parent,'product_cat');
如果($categories&!is_wp_error($category)):
回声“
    ”; foreach($类别作为$类别): $term=get_term($category,'product_cat'); 回音“
  • ”; 回声'; 回音“
  • ”; endforeach; 回声“
”; endif;
这只适用于您的档案。和儿童类别

它还将输出孙子类别


希望这能有所帮助。

这里有一种方法可以只获取按名称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>';
}
//产品类别分类法
$taxonomy='product_cat';
//获取父类别ID
$parent\u cat\u id=get\u terms($taxonomy,array)(
“父项”=>0,
“hide_empty”=>false,
“字段”=>“ID”
) );
//仅获取“子”WP_术语产品类别
$subcategories=获取术语($taxonomy,array)(
“排除”=>$parent\u cat\u id,
'orderby'=>'name',
“订单”=>“asc”,
“hide_empty”=>false,
) );
如果(!空($subcategories)){
回声“
    ”; foreach($子类别作为$子类别){ 回声'
  • '; } 回声“
”; }

代码已测试并生效

您可以共享一些示例查询结果吗?您不会将当前类别作为参数发送,而且您的SQL查询必须使用此参数筛选结果。如果您可以共享SQL查询,我们可以观察它。