我将使用get\\u categories和get\\u categority\\u children的组合来实现这一点。例如:
在主页上包括以下内容:
<?php
$args=array(
\'orderby\' => \'name\',
\'order\' => \'ASC\'
);
$categories=get_categories($args);
foreach($categories as $category) {
$catChildren = get_term_by($category->ID, \'category\');
if(!empty($catChildren)) {
echo \'<h3><a href=www.yourwebsite.com/"\' . $category->slug . \'">\' . $category->name . \'</h3>\';
}
}
?>
然后在子类别页面上:
<?php
$currentCat = get_the_category();
$args=array(
\'orderby\' => \'name\',
\'order\' => \'ASC\',
\'child_of\' => $currentCat->cat_ID
);
$categories=get_categories($args);
if($categories) {
foreach($categories as $category) {
echo \'<h3><a href=www.yourwebsite.com/"\' . $category->slug . \'">\' . $category->name . \'</h3>\';
}
}
else {
$args = array(
\'numberposts\' => 10,
\'category\' => $currentCat
);
$catPosts = get_posts( $args );
foreach ( $catPosts as $post ) {
setup_postdata($post);
the_title();
the_content();
}
}
?>
[我没有对此进行测试,但这是我之前的测试方式-显然,您需要更改一些细节。请务必阅读函数文档,以免最终受挫!]
您需要正确设置类别和“循环”才能使其工作[例如,必须从循环内调用,除非提供类别ID]。
确保阅读所有函数的文档,以了解它们正在执行的操作:get\\u category\\u children-http://hitchhackerguide.com/2011/02/12/get_category_children/get\\u类别-http://codex.wordpress.org/Function_Reference/get_categoriesget\\u帖子-http://codex.wordpress.org/Template_Tags/get_posts
如果您有问题,请发表评论,我将尽最大努力进一步阐述。