Hii我希望当我单击类别链接时,显示子类别(如果可用),而不是帖子列表。如果没有子类别可用,则显示普通帖子。要做到这一点,如果可用,子类别仅显示子类别,而不是任何其他帖子。请查看我在存档页面中添加的显示子类别(如果可用)的额外代码,否则显示帖子摘要
是否仅显示子类别(如果有)?
1 个回复
最合适的回答,由SO网友:Michael 整理而成
使用条件语句,例如:
$sub_cats = get_categories(\'parent=\'.get_query_var(\'cat\'));
if( $sub_cats ) :
//show list of child categories, for instance with wp_list_categories()
echo \'<ul>;
wp_list_categories(\'title_li=&child_of=\'.get_query_var(\'cat\'));
echo \'</ul>\';
//or possibly using $sub_cats and a foreach loop//
echo \'<ul>\';
foreach( $sub_cats as $sub_cat ) {
echo \'<li><a href="\'.get_category_link($sub_cat->term_id).\'">\'.$sub_cat->name.\'</a></li>\';
}
echo \'</ul>\';
else:
//the \'normal\' LOOP of category.php//
endif;
http://codex.wordpress.org/Function_Reference/get_categorieshttp://codex.wordpress.org/Template_Tags/wp_list_categories编辑:添加子类别的列表代码
结束