据我所知,有一个父类别和子类别的列表,就像一个菜单。对于第一个屏幕截图:
Cat1-SUBAT1-SUBAT2-SUBAT3
Cat2-SUBAT1-SUBAT2-SUBAT3
假设您只有两个级别的类别,您将使用它来显示您的类别。请注意,您可以在循环外部使用此选项:
<ul class="category-sidebar">
<?php
$get_parent_cats = array(
\'parent\' => \'0\' //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
$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></li>\';
} //end of categories logic ?>
</ul><!--end of category-sidebar-->
现在是第二个屏幕截图,我假设当您单击上面菜单中的父类别时,它将显示这样一个页面,其中包含每个子类别及其描述。
请参阅我在评论中留下的教程,了解如何在类别中使用条件。php,基本上您将在归档中执行的操作是检查父类别是正在加载还是子类别。如果是子类别,则显示帖子循环;如果是父类别,则显示包含说明的子类别页面。下面是循环中子类别及其描述的代码:
<?php
//for this category on an archive page, get the ID
$thisID = get_query_var(\'cat\');
$get_children_cats = array(
\'child_of\' => $thisID //get children of this parent using the thisID variable from earlier
);
$child_cats = get_categories( $get_children_cats );//get children of this parent category
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 and description
echo \'<div class="child-wrap"><h2><a href=" \' . get_category_link( $childID ) . \' ">\' . $child_cat->name . \'</a></h2><br/>\';
echo \'<p>\' . $child_cat->category_description . \'</p></div>\';
} //end of categories logic ?>
我知道这是很多代码,但看起来你需要做很多事情,你唯一要做的就是让归档文件在第二个屏幕截图中显示帖子列表或页面。希望这比我之前提供的评论中的资源更有用。祝你好运