按父母和子类别组显示Wordpress Post

时间:2014-12-22 10:31:17

标签: wordpress post categories custom-post-type

我正在使用wordpress插件。我为产品创建自定义帖子类型。然后我为我的帖子类型创建2个自定义类别。现在我想显示如下图像。任何人都可以帮忙。

enter image description here

1 个答案:

答案 0 :(得分:0)

<?php
    $args=array(
    'post_type'                => 'custom_post_type',
    'child_of'                 => 0,
    'parent'                   => '',
    'orderby'                  => 'name',
    'order'                    => 'ASC',
    'hide_empty'               => 1,
    'hierarchical'             => 1,
    'exclude'                  => '',
    'include'                  => '',
    'number'                   => '',
    'taxonomy'                 => 'custom_category',
    'pad_counts'               => false
    );  
    $categories=get_categories($args);
    echo '<ul>';
    foreach ( $categories as $category ) {
    if ( $category->parent > 0 ) {
        continue;
    }
            echo '<li><a href="' . get_category_link( $category) . '" title="' . sprintf( __( "View all posts in %s" ), $category->name ) . '" ' . '>' . $category->name.'</a>';
                $deptID = $category->cat_ID;
                $childCatID = $wpdb->get_col("SELECT term_id FROM $wpdb->term_taxonomy WHERE parent=$deptID");
                if ($childCatID){
                    echo '<ul>';
                    foreach ($childCatID as $kid)
                    {
                        $childCatName = $wpdb->get_row("SELECT name, term_id FROM $wpdb->terms WHERE term_id=$kid");
                        echo '<li><a href="' . get_term_link( intval($childCatName->term_id),  'my_product_cats' ) . '" title="' . sprintf( __( "View all posts in %s" ), $childCatName->name ) . '" ' . '>' . $childCatName->name.'</a>';
                        echo '<li><a href="#" title="' . sprintf( __( "View all posts in %s" ), $childCatName->name ) . '" ' . '>' . $childCatName->name.'</a>';
                    }
                    echo '</ul>';
                }
            echo '</li>';
    }
    echo '</ul>';
?>

请尝试此代码。它的类别如下:

分类

----子类别1

-------- Sub Sub Category 1

-------- sub Sub Category 2

----子类别2

-------- Sub Sub Category 1

-------- sub Sub Category 2