What determines which categories are shown on the single pages?
Thanks for getting back to me.
The categories are of the content itself (rather than all the categories in the website). For example, the categories of this content: http://doubleagents.webfactional.com/site/archives/171 is Camden Arts Centre, Event, etc. Essentially the same as the_category(), but I can’t get this to organise itself well.
Does that make sense?
Thanks,
Andrew
Think I have almost worked it all out… now just need to get that link working properly. I will post code later…
This is the code I’m using, to show a post’s set of nested sub-categories in a definition list. It seems to be working, but it could probably be done more efficiently.
Notes:
1. the parent categories aren’t links, but could easily be made to be
2. it wouldn’t work well for sub-sub-categories (not an issue for this website)
<dl id="categories">
<?php
foreach((get_the_category()) as $cat) {
if ($cat->category_parent == 0) {
// root/parent category (don't do anything - this should not have been a link - but it could be if that's the way you wanted it set-up)
} else {
// show category link
// get category_parent details (nicename and cat_name required)
// only show parent category if it is first one
if ($cat->category_parent != $oldCatParentId) {
$catParentName = get_cat_name( $cat->category_parent );
echo '<dt>'.$catParentName.':</dt>';
}
$oldCatParentId = $cat->category_parent;
echo '<dd><a href="'.get_category_link($cat->cat_ID).'">'. $cat->cat_name . '</a>' . '</dd>';
}
} ?>
</dl> <!-- /#categories -->
Run into problem: the categories are not in nested order.
Problem here: http://doubleagents.webfactional.com/site/archives/143
Any ideas?
Took me a bit to figure out what you’ve got going on there –
So, are you trying to display all child categories of a parent category together (So you dont get multiple lists of contributors, as you have on the page you showed)?
Hi,
Yeah the problem I have now is that sub-categories aren’t grouped by parent categories.
For example, on this page’s sidebar the parent category ‘Contributors’ is split into 6 sections. I need to do a php usort (?) on the object retrieved from get_the_category() so it can be ordered by category_parent. But my php skills lack in this area.
Any ideas?
Okay – this is a way of doing it (if anyone else is interested)…
<dl id="categories">
<?php
$organisedCats = get_the_category();
$catOutput = "";
$catArray = array();
foreach((get_the_category()) as $cat) {
if ($cat->category_parent == 0) {
// root/parent category (don't do anything - this should not have been chosen)
} else {
if ($catArray[$cat->category_parent]) {
// if the array bit already exists, then dobn't do anything
} else {
// write the title of the category
$catParentName = get_cat_name( $cat->category_parent );
$catArray[$cat->category_parent] = '<dt>'.$catParentName.':</dt>';
}
// write in the specific category
$catArray[$cat->category_parent] .= '<dd><a href="'.get_category_link($cat->cat_ID).'">'. $cat->cat_name . '</a>' . '</dd>';
}
}
foreach($catArray as $cat) {
echo $cat;
}
?>
</dl> <!-- /#categories -->
This is a great post!
I need this to work with sub-sub-categories as well though.
Any idea how?