I thought I would come in here and share this. This was such an unusual request, and so unique and interesting, that I couldn't NOT share it. It took me all day to figure this out and code it (and I'm still willing to bet it could be coded more cleanly) and I used bits and pieces of code I found here on the forums and in the Codex.
So here's the deal. Client wanted me to have a sidebar that had the following criteria:
1) when you were on any category listing, it would list links to the subcategories of that parent.
2) that had to work for all sub-categories (grandchildren, great-grandchildren...) too. The topmost parent category would show, with a list of sub-, sub-sub-, (on and on) links.
both of these could be done with wp_list_categories but the kicker was this: if you were on a single post page, it still had to do it AND it had to list all the post titles only in that category. Meaning, it couldn't list parent titles, not children. Only ones in that specific category.
I have to say, there was a point where I thought I wouldn't be able to do it... but I did. So here you go.
function list_page_children() {
global $post, $wp_query;
$cat = $wp_query->get_queried_object();
if(is_category()) {
$id = $cat->term_id;
$current_name = $cat->slug;
if($id > '0') {
$slugs = get_category_parents($cat, FALSE, ',', TRUE);
$getparentslugs = explode(',',$slugs);
$slug = $getparentslugs[0];
$names = get_category_parents($cat, FALSE, ',', FALSE);
$getparentnames = explode(',',$names);
$parent = $getparentnames[0];
$getparentid = get_category_by_slug($slug);
$parentid = $getparentid->term_id;
}
$thelist = wp_list_categories('title_li=&echo=0&child_of=' . $parentid) . "\n";
} else if(is_single()) {
$category = get_the_category();
$parent = $category[0]->cat_name;
$postcatid = $category[0]->term_id;
$postcatcount = $category[0]->count;
if($postcatid > '0') {
$slugs = get_category_parents($postcatid, FALSE, ',', TRUE);
$getparentslugs = explode(',',$slugs);
$slug = $getparentslugs[0];
$names = get_category_parents($postcatid, FALSE, ',', FALSE);
$getparentnames = explode(',',$names);
$parent = $getparentnames[0];
$getparentid = get_category_by_slug($slug);
$parentid = $getparentid->term_id;
}
$getsubs = get_categories('child_of=' . $parentid);
if($getsubs) {
$categories = $getsubs;
$isparent = 'no';
} else {
$categories = get_the_category();
$isparent = 'yes';
}
if($categories) {
foreach($categories as $cat) {
if ($cat->count > '0') {
if($isparent != 'yes') {
$thelist .= '<li><a href="' . get_category_link( $cat->term_id) . '">' . $cat->cat_name . '</a>' . "\n";
}
$numposts = -1;
$posts = get_posts('cat='. $cat->term_id . '&showposts=' . $numposts);
if($posts) {
if($cat->term_id == $postcatid) {
$thelist .= '<ul>' . "\n";
foreach($posts as $post) {
setup_postdata($post);
$thelist .= '<li><a href="' . get_permalink($post->ID) . '" rel="bookmark" title="Permanent Link to ' . get_the_title() . '">' . get_the_title() . '</a></li>' . "\n";
}
$thelist .= '</ul>' . "\n";
}
}
$thelist .= '</li>' . "\n";
}
}
}
}
$empty = 'No categories';
$find = strpos($thelist, $empty);
if($find != '') {
$thelist = '';
}
?>
<h3><?php echo $parent; ?></h3>
<ul id="children">
<?php echo $thelist; ?>
</ul>
<?php
}
Like I said, some of the code is redundant - probably could use some cleaning up. If anyone wants to take that on, more power to ya - but his works a treat. I'm all proud of myself.
Hope it helps someone :)