Showing Post Titles Within the Category Listing

I had an interesting dilemma over the weekend. Someone sent me a comp to code out (PSD->WordPress), which isn’t anything shocking since I do that a lot. What the dilemma was: in the sidebar, they wanted a category listing. However they wanted only the category listing for the category page you were on. So, say you’re in the “Dogs” category… they wanted the sidebar to have “Dogs” (the current category name) at he top, and have all the child categories listed beneath that. They also wanted it that way on every page: so if you were in “Shih-Tzu”, they wanted the “Dogs” full listing to be in the sidebar.)

Now, this, I’ve done before. No problem. WordPress’ 2.8 system of adding the body class has made my old code moot, and I need to report a new way of pulling this off.

So where’s the dilemma, you ask?

It appeared that the client also wanted whatever category link you were on to be “open”, and list all the posts titles in that category.

I say “appeared,” because it dawned on me last night as I was loading the theme up on the site that they just wanted the category/children listing as described above without the post listing added in. Something that’s pretty simple to do. I had misunderstood what they wanted. BUT because of my mistake, and my intent to make it happen, I did actually pull off making the post titles show up under the category you are in. So, you get something out of this that I don’t have anywhere to show it.

There were several issues with pulling this off. There’s no method in wp_list_categories() that allows for this. I know there’s some way to add an array to an already written function, but that level of PHP coding is beyond me (for now). if I could have figured that out, this code would probably be tons cleaner and easier to use. I’m also quite sure the code I did come up with is a bit…expanded…and could be tightened up. (I had a deadline – when it worked I was just freaking happy it worked.) So please don’t be harsh. However, if you are a PHP expert, feel free to add in suggestion to tighten this up and make it less messy. :)

Another issue was that once the child category opened up with its posts, it would also list the posts that were in the grandchild category. That one was a toughie. I searched he forums and saw all kinds of posts regarding similar issues – with no solutions. So when I figured out how to ONLY make the posts within that particular category (not its parent, not its children) I was… to use a lame term… stoked.
Please remember that sometimes WordPress formats quotes. So if this throws up some kind of error or something, double-check the quotes you copied… they might be WordPress-ified. Yeah, yeah – I know. I need to get a plugin that displays code better. I will, I promise.
Without further ado, here’s the code. Please use it safely.

<?php 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);
$postlink = get_the_ID();
if($postlink == $thispost) {
$linkclass = ' class="current-post"';
} else {
$linkclass = '';
}
if(in_category($cat)) {
$thelist .= '<li class="' . $class . '">' . 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 } ?>

Have your say:

* Copy this password:

* Type or paste password here:

1,988 Spam Comments Blocked so far by Spam Free Wordpress