WordPress从子类别列出帖子

时间:2012-01-22 作者:Uffo

有人有主意吗?你怎么能这样做:

因此,如果用户属于某个类别Main这个类别有一些子类别,我想列出它们,并列出帖子,类似这样:

Sub category name 1

-职位1

-职位2

Sub category name 2

-职位1

-职位2

Sub category name 3

-职位1

-职位2

1 个回复
SO网友:Uffo

我找到了这段代码,它可以工作:

<?php get_header(); ?>
<div id="job-listings">
<?php
            $category = get_the_category();
            $theName = $category[0]->name;
            $theChild = $category[0]->cat_ID;
$subcats = get_categories(\'child_of=\' . $theChild);
    foreach($subcats as $subcat) {
    echo \'<h3>\' . $subcat->cat_name . \'</h3>\';
    echo \'<ul>\';
    $subcat_posts = get_posts(\'cat=\' . $subcat->cat_ID);
    foreach($subcat_posts as $subcat_post) {
        $postID = $subcat_post->ID;
    echo \'<li>\';
    echo \'<a href="\' . get_permalink($postID) . \'">\';
    echo get_the_title($postID);
    echo \'</a></li>\';
    }
    echo \'</ul>\';
    } ?>
 </div>
 <?php get_footer(); ?>

结束