As there doesn't seem to be an adequate and still actively developed WordPress plug-in that allows me to display a collapsible category tree without having to use widgets (I personally dislike widgets), I decided to write one on my own.
I wrote this code:
<script type="text/javascript">
function toggleCatDiv(id) {
if (jQuery("#catTogglerDiv-"+id).is(":visible")) {
jQuery("#catToggler-"+id).innerHTML="►";
}
else {
jQuery("#catToggler-"+id).innerHTML="▼";
}
jQuery("#catTogglerDIV-"+id).slideToggle("normal");
}
</script>
<?php
$args = array('orderby' => 'name', 'parent' => 0 );
$categories = get_categories( $args );
foreach ( $categories as $category ) {
?>
<span style="cursor:pointer" id="catToggler-<?php echo $category->cat_ID; ?>" onclick="toggleCatDiv('<?php echo $category->cat_ID; ?>')">►</span> <a href="<?php echo get_category_link( $category->term_id ); ?>"><?php echo $category->name;?></a><br />
<div id="catTogglerDIV-<?php echo $category->cat_ID; ?>" style="margin-left:3em;visibility:hidden">
<?php
$args = array(
'child_of' => $category->cat_ID
);
$subcats = get_categories($args);
foreach ( $subcats as $subcat ) {
<a href="<?php echo get_category_link( $subcat->term_id ); ?>"><?php echo $subcat->name;?></a>
}
?>
</div>
<?php
}
?>
As you might see, it's intended to grab the list of "main categories" and display a list of subcategories when clicking the arrow on each of them.
Now clicking the arrow does nothing (not even a JS error), and only 2 of 4 main categories are actually shown. Why?