下面的代码基于几个地方的几个论坛帖子。在某种程度上,它是有效的。我遇到的问题是,它只显示“活动”,无论我属于哪一类家长。我尝试用一些非常不成功的替代品(条件、get函数等)替换“活动”;什么都没用。
<?php $cat = \'Activities\'; /* This is the problem area.
Trying to get $cat to equal the name of the selected parent categories; the rest of
the code seems to be working, at least for the Activities category. */
$catID = get_cat_ID($cat);
echo \'<h2>\' . $cat . \'<h2>\';
$subcats = get_categories(\'child_of=\' . $catID);
foreach($subcats as $subcat)
{
echo \'<h4>\' . $subcat->cat_name . \' </h4>\';
echo \'<ul>\';
$subcat_posts = get_posts(\'cat=\' . $subcat->cat_ID);
foreach($subcat_posts as $subcat_post)
{
echo \'<li>\';
$postID = $subcat_post->ID;
echo \'<a href="\' . get_permalink($postID) . \'">\';
echo get_the_title($postID);
echo \'</a>\';
echo \'</li>\';
}
echo \'</ul>\';
}?>