如何在一个模板中获取下级类别列表帖子?

时间:2011-05-15 作者:idontknowhow

这是我想要的

WP admin中的我的类别结构

Cat Parent
  -Cat Child 1
  -Cat Child 2
  -Cat Child 3
  -Cat Child 1
这是我希望在一个模板中包含的内容:

它显示Cat父子类别中的帖子列表

Cat Child 1
 -Post1 Child 1
 -Post2 Child 1
 -Post3 Child 1
 -So on... .

Cat Child 2
 -Post1 Child 2
 -Post2 Child 2
 -Post3 Child 2
 -So on... .

Cat Child 3
 -Post1 Child 3
 -Post2 Child 3
 -Post3 Child 3
 -So on... .
以前有人做过吗,或者有人知道如何编写代码吗?

谢谢

3 个回复
最合适的回答,由SO网友:Michael 整理而成

获取子类别,使用get_categories();然后使用foreach循环遍历它们,使用WP_Query() :

<?php  $cats = get_categories(\'child_of=\'.get_query_var(\'cat\')); 

    foreach ($cats as $cat) :

    $args = array(
    \'posts_per_page\' => 3, // max number of post per category
    \'category__in\' => array($cat->term_id)
    );
    $my_query = new WP_Query($args); 

        if ($my_query->have_posts()) : 
        echo \'<h3>\'.$cat->name.\'</h3>\';

        while ($my_query->have_posts()) : $my_query->the_post(); ?>     
        <?php /*general loop output; for instance: */ ?>
        <a href="<?php the_permalink() ?>"><?php the_title(); ?></a>    <br />  

        <?php endwhile; ?>

        <?php else : 
        echo \'No Posts for \'.$cat->name;                
        endif; 

    endforeach; ?>

SO网友:Ayan Chakraborty

Parent child taxonomy loop

$taxonomy = \'product_cat\';
$category = get_terms($taxonomy);

echo \'<ul>\';

foreach( $category as $cat ){
    $link = get_term_link( $cat->slug, $taxonomy );

    if(empty($cat->parent)){
        echo \'<li><a href="\' . $link . \'">\' . $cat->name . \'</a>\' . \'<strong>\' . $cat->count . \'</strong></li>\';

    }
    $loop = 0;

    foreach( $category as $par ){
        $link = get_term_link( $par->slug, $taxonomy );
        if($cat->term_id == $par->parent ){

        if($loop == 0){ echo \'<ul>\'; }
        echo \'<li><a href="\' . $link . \'">\' . $par->name . \'</a>\' . $par->count . \'</li>\';

        $loop++;
        }
    }
    if($loop > 0){ echo \'</ul>\'; }

}

echo \'</ul>\';
SO网友:Pavan
<ul class="catTags">
<?php
        $args = array(
        \'show_option_all\'    => \'\',
        \'orderby\'            => \'count\',
        \'order\'              => \'DESC\',
        \'style\'              => \'list\',
        \'show_count\'         => 0,
        \'hide_empty\'         => 1,
        \'use_desc_for_title\' => 1,
        \'child_of\'           => 0,
        \'feed\'               => \'\',
        \'feed_type\'          => \'\',
        \'feed_image\'         => \'\',
        \'exclude\'            => 1,
        \'exclude_tree\'       => \'\',
        \'include\'            => \'\',
        \'hierarchical\'       => 1,
        \'title_li\'           => __( \'\' ),
        \'show_option_none\'   => __(\'No categories\'),
        \'number\'             => null,
        \'echo\'               => 1,
        \'depth\'              => 2,
        \'current_category\'   => 0,
        \'pad_counts\'         => 0,
        \'taxonomy\'           => \'category\',
        \'walker\'             => null    
        ); 

        wp_list_categories( $args );
    ?>
    </ul>   
    </div>

    <?php get_sidebar(); ?>

And the CSS :

section .primary ul.catTags {
 padding:0px;
}

section .primary ul.catTags li {
 float: left;
 list-style-type: none;
 margin: 0px 20px 10px 20px;

}

section .primary ul.catTags li a,section .primary ul.catTags li a:visited {
 font-size:18px;
 text-decoration:underline;
}
section .primary ul.catTags li a:hover {
 text-decoration:none;
}
section .primary ul.catTags li ul.children {
 max-width:200px;
}
section .primary ul.catTags li ul.children li {
 margin: 0px 5px 3px 5px;
 display: inline;
}
section .primary ul.catTags li ul.children li a, section .primary ul.catTags li ul.children li a:visited {
 font-size:14px;
 text-decoration:none;
}
section .primary ul.catTags li ul.children li a:hover {
 text-decoration:underline;
}
结束