Forum Moderators: coopster
This is the code:
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;
$term = $id;
} else if(is_single()) {
$category = get_the_category();
$parent = $category[0]->cat_name;
$postcatid = $category[0]->term_id;
$postcatcount = $category[0]->count;
$term = $postcatid;
}
if($term > '0') {
$names = get_category_parents($term, FALSE, ',', FALSE);
$getparentnames = explode(',',$names);
$parent = $getparentnames[0];
$slugs = get_category_parents($term, FALSE, ',', TRUE);
$getparentslugs = explode(',',$slugs);
$slug = $getparentslugs[0];
$getparentid = get_category_by_slug($slug);
$parentid = $getparentid->term_id;
}
$getsubs = get_categories('child_of=' . $parentid);
if($getsubs) {
$categories = $getsubs;
} else {
$categories = get_the_category();
}
// current category stuff
$thiscatid = get_query_var('cat');
$thiscat = &get_category($thiscatid);
$thiscatname = $thiscat->slug;
$thiscatid = $thiscat->term_id;
$thiscatparent = $thiscat->parent;
$thispost = $post->ID;
if($categories) {
foreach($categories as $cat) {
$postcount = $cat->count;
$catname = $cat->cat_name;
$catid = $cat->term_id;
$catparent = $cat->parent;
$numposts = -1;
// category classes
if(is_category()) {
$current_category = $thiscatid;
$current_parent = $thiscatparent;
$class = 'cat-item cat-item-'.$cat->term_id;
if (isset($current_category) && $current_category && ($catid == $current_category)) {
$class .= ' current-cat';
} else if(isset($current_category) && $current_category && ($catid == $current_parent)) {
$class .= ' current-cat-parent';
}
} else if(is_single()) {
$current_category = $postcatid;
$current_parent = $catid;
$class = 'cat-item cat-item-'.$cat->term_id;
if (isset($current_category) && $current_category && ($catid == $current_category)) {
$class .= ' current-post-cat';
}
}
$thelist .= '<li class="' . $class . '"><a href="' . get_category_link( $catid) . '">' . $catname . '</a>' . "\n";
$posts = get_posts('cat=' . $catid . '&showposts=' . $numposts);
if($posts) {
$thelist .= '<ul>' . "\n";
if($postcount != '0') {
foreach($posts as $post) {
setup_postdata($post);
if(is_single()) {
$postlink = get_the_ID();
if($postlink == $thispost) {
$linkclass = ' current-post';
} else {
$linkclass = '';
}
}
$thelist .= '<li class="' . $class . $linkclass . '"><a href="' . get_permalink($post->ID) . '" rel="bookmark" title="Permanent Link to ' . get_the_title() . '">' . get_the_title() . '</a></li>' . "\n";
} // end foreach
$thelist .= '</ul></li>' . "\n\n";
} // end "if postcount"
// $thelist .= '</li>' . "\n\n";
} // end "if posts"
} // end foreach
} // end "if categories"
$empty = 'No categories';
$find = strpos($thelist, $empty);
if($find != '') {
$thelist = '';
} ?>
<h3><?php echo $parent; ?></h3>
<ul id="children">
<?php echo $thelist; ?>
</ul>
<?php
} The problem is in the section where I start putting in UL tags. The code above works perfectly *except* that it's not closing out a grouping in the proper place. The above code will output the following example:
(this is cleaned up for easier reading - just a visual to see what happens clearly)
<h3>Toplevel Category Name</h3>
<ul id="children">
<li>Category 1
<ul>
<li>Subcategory 1
<ul>
<li>post title in Subcategory 1</li>
<li>post title in Subcategory 1</li>
</ul></li>
<li>Subcategory 2
<ul>
<li>post title in Subcategory 2</li>
<li>post title in Subcategory 2</li>
<li>post title in Subcategory 2</li>
</ul></li>
<li>Category 2
<ul>
<li>post title in Category 2</li>
<li>post title in Category 2</li>
<li>post title in Category 2</li>
<li>post title in Category 2</li>
</ul></li>
</ul> I've moved things around a million times, and I can't get a
</li> tag where it's supposed to be - it either goes in the wrong place, or it adds one to every list item. Can anyone see what I'm missing? I'd appreciate and extra pair of eyes. Thank you!
// $thelist .= '</li>' . "X1\n\n";
$thelist .= 'X1';
} // end "if posts"
$thelist .= 'X2';
} // end foreach
$thelist .= 'X3';
} // end "if categories"
$thelist .= 'X4';
Run that and see if any of the X's mark the spot.
in case anyone else needs it, here's the finished code:
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;
$term = $id;
} else if(is_single()) {
$category = get_the_category();
$parent = $category[0]->cat_name;
$postcatid = $category[0]->term_id;
$postcatcount = $category[0]->count;
$term = $postcatid;
}
if($term > '0') {
$names = get_category_parents($term, FALSE, ',', FALSE);
$getparentnames = explode(',',$names);
$parent = $getparentnames[0];
$slugs = get_category_parents($term, FALSE, ',', TRUE);
$getparentslugs = explode(',',$slugs);
$slug = $getparentslugs[0];
$getparentid = get_category_by_slug($slug);
$parentid = $getparentid->term_id;
}
$getsubs = get_categories('child_of=' . $parentid);
if($getsubs) {
$categories = $getsubs;
} else {
$categories = get_the_category();
}
// current category stuff
$thiscatid = get_query_var('cat');
$thiscat = &get_category($thiscatid);
$thiscatname = $thiscat->slug;
$thiscatid = $thiscat->term_id;
$thiscatparent = $thiscat->parent;
$thispost = $post->ID;
if($categories) {
foreach($categories as $cat) {
$postcount = $cat->count;
$catname = $cat->cat_name;
$catid = $cat->term_id;
$catparent = $cat->parent;
$numposts = -1;
// category classes
if(is_category()) {
$current_category = $thiscatid;
$current_parent = $thiscatparent;
$class = 'cat-item cat-item-'.$cat->term_id;
if (isset($current_category) && $current_category && ($catid == $current_category)) {
$class .= ' current-cat';
} else if(isset($current_category) && $current_category && ($catid == $current_parent)) {
$class .= ' current-cat-parent';
}
} else if(is_single()) {
$current_category = $postcatid;
$current_parent = $catid;
$class = 'cat-item cat-item-'.$cat->term_id;
if (isset($current_category) && $current_category && ($catid == $current_category)) {
$class .= ' current-post-cat';
}
}
$term = get_categories('pad_counts=1');
foreach ($term as $term) {
$termid = $term->term_id;
$termcount = $term->count;
}
$thelist .= '<li class="' . $class . '"><a href="' . get_category_link( $catid) . '">' . $catname . '</a>' . "\n";
$posts = get_posts('cat=' . $catid . '&showposts=' . $numposts);
if($posts) {
$thelist .= '<ul>' . "\n";
$catpostcount = count($posts); // counts all the posts in the category
if($postcount != '0') {
foreach($posts as $post) {
setup_postdata($post);
if(is_single()) {
$postlink = get_the_ID();
if($postlink == $thispost) {
$linkclass = ' current-post';
} else {
$linkclass = '';
}
}
$count = $postcount;
$thelist .= '<li class="' . $class . $linkclass . '"><a href="' . get_permalink($post->ID) . '" rel="bookmark" title="Permanent Link to ' . get_the_title() . '">' . get_the_title() . '</a></li>' . "\n";
} // end foreach
$thelist .= '</ul></li>' . "\n\n";
} // end "if postcount"
if($postcount == $termcount) {
$thelist .= '</ul></li>' . "\n";
}
} // end "if posts"
} // end foreach
} // end "if categories"
$empty = 'No categories';
$find = strpos($thelist, $empty);
if($find != '') {
$thelist = '';
} ?>
<h3><?php echo $parent; ?></h3>
<ul id="children">
<?php echo $thelist; ?>
</ul>
<?php
}