Main and secondary categories in MT (with a little help from PHP)

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.

11 Responses to “Main and secondary categories in MT (with a little help from PHP)”

  1. Christine Says:

    Just yesterday I discovered that David Raynes has released his subcategory hack at:
    http://www.rayners.org/2003/08/19/subcategories.php

    I found it at Jay Allen’s site, and he added additional “recipes” for it which he offers here:
    http://www.jayallen.org/misc/tutorials/subcat_mt_plugin/

    His full post about it is here:
    http://www.jayallen.org/journey/2003/09/subcategories_plugin_for_mt

    (I was going to post all of this here, but realized it tied in to this post. I can make it a separate one if you want me too.)

  2. ceejayoz Says:

    You really shouldn’t use PHP’s short tags - use the full <?php ?> notation.

    Short tags are often disabled and thus will cause problems on some sites.

  3. Garinungkadol Says:
    MTCategory Bliss!
    David Raynes has come out with a subcategory plugin while the lovely gals at scriptygoddess have come up with this…

  4. Patti Says:

    hi good morning. i have to be honest in saying that i am pretty overwhelmed…

    Parse error: parse error in /home2/blh2o3/bicusa-www/bicusa/issues/africa/index.php on line 200

    site: http://www.bicusa.org/bicusa/

    site map: http://www.bicusa.org/bicusa/categories.html

    code is located at :
    http://www.bicusa.org/bicusa/categoty_archive.txt

    yes, i did modify the original code.

    i also made a post at moveabletype.org, but i didn’t explain myself very clearly. i figured since this is your script, you would know best.

    any help on this would be MUCH MUCH appreciated. thank you.

    best,
    patti

  5. Patti Says:

    Ooops… I forgot to add the link to my moveabletype.org post.

    post on moveabletype.org

  6. Patti Says:

    i’m so sorry, last one for sure…

    a .txt file version of the php Category Archive page that has the error.

  7. Christopher Trott Says:

    I am trying to list my categories via Movable Type. What tag do I use? any help would be greatly appericated.

    chris

  8. Christopher Trott Says:

    I am trying to list my categories via Movable Type. What tag do I use? any help would be greatly appericated.

    chris

  9. david arthurs Says:

    Can you tell me where you place that code? I am relatively new to MT.

    Do you have to create a seperate tempate for each category and then place that code in each category template code?

  10. david Says:

    how do you get the rest of your main menu categories (that don’t have sub cateoires to show) to show ? and if you use that script on the index.phph page it seems to show the full category name with hypen and subcategy etc., - how do you just get the main category ‘Appearances’ to show on the homepage?

  11. Jennifer Says:

    David - you can put that code in any template you want the list to display in. (You don’t need a seperate template) - as for your other questions - I’m not following what you’re doing. If you can email me the code you have on your page, and a link to where it’s displaying - I might be able to help. (email is: scripty AT scriptygoddess DOT com)

    Christopher - “I am trying to list my categories via Movable Type.” You can find that answer in the movable type manual.