从wp_list_categories中删除顶级类别

时间:2013-11-26 16:19:40

标签: wordpress

假设我有一个类别列表

我试图隐藏顶级类别(P1,P2 ......),但仍然保持列表的层次结构。

P1
-Ch1P1
--Ch1.1P1
--Ch2.1P1
--Ch3.1P1
-Ch2P1
--Ch1.2P1
--Ch2.2P1
-Ch3P1
--Ch1.3P1
P2
-Ch1P2
--Ch1.1P2
--Ch1.2P2
-Ch2P2
--Ch1.2P2
-Ch3P2
P3
-Ch1P3
--Ch1.1P3
--Ch2.1P3
-Ch2P3
--Ch1.2P3
.
.
.

如何列出没有顶级父级(没有P1,P2,P3)的层次结构???


更新

这是我的代码:

<?php
// Get Current post ID (BRAND SLUG)
$BrandSlug = get_queried_object()->slug;
$BrandName = get_queried_object()->name;

// List of Categories prom This BRAND
$args = array('post_type' => 'product', 'brands'=>$BrandSlug ,'post_status' => 'publish', 'posts_per_page'=>'1000');
$products = get_posts( $args );

// Defining necessary arrays
$ProductCategories = array();
$ProductCategoryParent = array();

foreach( $products as $product ) : setup_postdata($post);

    $Categories = get_the_terms( $product->ID, 'product-category' );

    foreach ( $Categories as $Category ) {
        //if( $Category->parent > 0 ){
        $ProductCategories[] = $Category->term_id;
        //}                 
    }

endforeach;

$cargs = array(
        'show_option_all'    => false,
        //'orderby'            => 'slug',
        //'order'              => 'DESC',
        'style'              => 'list',
        'show_count'         => 0,
        'hide_empty'         => 1,
        'use_desc_for_title' => 1,
        //'child_of'           => 201,
        //'feed'               => '',
        //'feed_type'          => '',
        //'feed_image'         => '',
        //'exclude'            => '197',
        //'exclude_tree'       => '197',
        'include'            => $include,
        'hierarchical'       => true,
        'title_li'           => 'Current Brand (<b>'.$BrandName.'</b>)',
        'show_option_none'   => 'No Categories',
        'number'             => null,
        //'echo'               => $visible,
        'depth'              => 0,
        //'current_category'   => $myCat,
        //'pad_counts'         => 0,
        'taxonomy'           => 'product-category',
        'walker'             => null
    );
echo '<ul id="brand_categories">';          
    wp_list_categories($cargs);
echo '</ul>';

?>

我的想法是,我可以在列出的类别中查看具有当前品牌产品的类别。

1 个答案:

答案 0 :(得分:1)

您想使用:

 &child_of=Category_id   

因此,如果您的类别ID为9,并且您希望显示该类别所有子项的无序列表,则可以执行以下操作:

<ul> 
  <?php wp_list_categories('orderby=id&show_count=1&use_desc_for_title=0&child_of=9'); ?>
</ul>

来自http://codex.wordpress.org/Template_Tags/wp_list_categories