20 Sep, 2003
Main and secondary categories in MT (with a little help from PHP)
Posted by: Jennifer In: Script snippet
The point of this script-snippet is so you can set up two hierarchies of categories. So there's a main category, and a few "sub-categories" below it. An example of this is on Christine's Pixelog site, on her album page.
This differs from the way MT has "Primary" and "Secondary" categories in that in mine, the sub-categories for a main category, can only be a sub category of that one main category – not any other main category. Here's another example: Ohnozone.net . In the sidebar, scroll down to the "categories", You'll see there's a main category "Appearances", and there's sub categories beneath it. "Interviews & Articles" and "TV"
So the biggest trick of this is that you name your categories in a consistent manner. In the example at Ohnozone – the categories are "Appearances – Interviews & Articles". The next category is "Appearances – TV". So the "Main" category is always the first part of your category name.
When we go to display the list, for each main category, you need the following block:
<?
$sortingCategory = "MAIN CATEGORY";
$removeAddtlnchars = SOMENUMBER-NOQUOTES!;
//————————————–
$numchars = strlen($sortingCategory);
$displayafterchars = $numchars + $removeAddtlnchars;
?>
<p><b><? echo $sortingCategory; ?></b></br>
<MTArchiveList archive_type="Category">
<?
$cat = "<$MTArchiveCategory$>";
if (!strncmp($cat, $sortingCategory, $numchars))
{
print ('<a href="<$MTArchiveLink$>">'.substr("<$MTArchiveCategory$>",$displayafterchars).'</a></br>');
}
?>
</MTArchiveList>
<p>
You'll only be modifying what's in bold/red. So here's how this works. Let's use the example of this Main/Sub category:
Appearances – Interviews & Articles
We'd set the value of $sortingCategory to "Appearances". In this example, we're seperating the "Main" category from the "Sub" category with " – " (a space, then a dash, then another space) that's three "characters". So we set the value of $removeAddtlnchars to 3.
Copy that block, and make those two adjustments for each "main" category you have.