Tìm theo từ khóa

Liệt kê tất cả danh mục con của một danh mục trong wordpress



Đoạn code bên dưới liệt kê tất cả danh mục con của một danh mục

$args = array('child_of' => 17);
$categories = get_categories( $args );
foreach($categories as $category) { 
    echo '<p>Category: <a href="' . get_category_link( $category->term_id ) . '" title="' . sprintf( __( "View all posts in %s" ), $category->name ) . '" ' . '>' . $category->name.'</a> </p> ';
    echo '<p> Description:'. $category->description . '</p>';
    echo '<p> Post Count: '. $category->count . '</p>';  
}

Nếu bạn chỉ muốn liệt kê danh mục con thôi thì cùng đoạn code sau

$args = array('parent' => 17);
$categories = get_categories( $args );
foreach($categories as $category) { 
    echo '<p>Category: <a href="' . get_category_link( $category->term_id ) . '" title="' . sprintf( __( "View all posts in %s" ), $category->name ) . '" ' . '>' . $category->name.'</a> </p> ';
    echo '<p> Description:'. $category->description . '</p>';
    echo '<p> Post Count: '. $category->count . '</p>';  
}

Liệt kê tất cả thư mục con và con của thư mục, danh sách bài viết con ta dùng đoạn code như sau

?> <ul>   
        <?php 
            $get_parent_cats = array(
                'parent' => '24' //get top level categories only
            ); 

            $all_categories = get_categories( $get_parent_cats );//get parent categories 

            foreach( $all_categories as $single_category ){
                //for each category, get the ID
                $catID = $single_category->cat_ID;

                echo '<li><a href=" ' . get_category_link( $catID ) . ' ">' . $single_category->name . '</a>'; //category name & link
                 echo '<ul class="post-title">';

                $query = new WP_Query( array( 'cat'=> $catID, 'posts_per_page'=>10 ) );
                while( $query->have_posts() ):$query->the_post();
                 echo '<li><a href="'.get_the_permalink().'">'.get_the_title().'</a></li>';
                endwhile;
                wp_reset_postdata();

                echo '</ul>';
                $get_children_cats = array(
                    'child_of' => $catID //get children of this parent using the catID variable from earlier
                );

                $child_cats = get_categories( $get_children_cats );//get children of parent category
                echo '<ul class="children">';
                    foreach( $child_cats as $child_cat ){
                        //for each child category, get the ID
                        $childID = $child_cat->cat_ID;

                        //for each child category, give us the link and name
                        echo '<a href=" ' . get_category_link( $childID ) . ' ">' . $child_cat->name . '</a>';

                         echo '<ul class="post-title">';

                        $query = new WP_Query( array( 'cat'=> $childID, 'posts_per_page'=>10 ) );
                        while( $query->have_posts() ):$query->the_post();
                         echo '<li><a href="'.get_the_permalink().'">'.get_the_title().'</a></li>';
                        endwhile;
                        wp_reset_postdata();

                        echo '</ul>';

                    }
                echo '</ul></li>';
            } //end of categories logic ?>
    </ul> <?php

 

Đánh giá nội dung này
0 0 đánh giá
Đánh giá bài viết
Theo dõi
Thông báo của
guest
0 Góp ý
Mới nhất
Cũ nhất Được bỏ phiếu nhiều nhất
0
Rất thích suy nghĩ của bạn, hãy bình luận.x