next/previous category archive

Thanks to Lynda and Jenn there is a way to navigate between next and previous entries only within a category. But there has been many requests on the forums for a way to go between the category archives with next and previous like the date-based archives have.

This script will show the linked title of the previous and next categories, alphabetically, on each category archive page.

This is a PHP-based solution. The pages that you put this on should be named with a .php extension. It requires that to parse the code, and won’t show anything in this section otherwise. To set your Archives to be .php, follow these steps:

  • Change the extension of the archives by going into the Blog Config under Preferences and entering php in the archive extension box.

  • Rebuild all.
  • Log into your server with your FTP client and delete the archives and index files which are .html. This will insure that the browser goes to the php versions.

Paste this code into the Category Archive Template, wherever you’d like the navigation to appear.

Required: MTGlue Plugin (available for MT2.21 and higher) to make the array easier to work with.

<?
//If you use an Archive Filename, you can use these fields to put what goes before
//and after the category name. So if your files are in category/index.php,
//put “category/” in the prefix and nothing in the suffix. Extention will be .php
//The default cat_categoryname.php is default.
$filename_prefix = “cat_”;
$filename_suffix = “”;

//This is the separator which goes between the titles and MAIN link.
$separator = ” | “;

$archpath = “<$MTBlogArchiveURL$>”;
$mainpath = “<a href=\”<$MTBlogURL$>\”>main</a>”;
$file_pre = “<a href=\”".$archpath.$filename_prefix;
$file_suf = $filename_suffix.”.php\”>”;
$thisarch = “<$MTArchiveTitle dirify=”1″$>”;

$categories = array(<MTGlueContainer><MTArchiveList archive_type=”Category”>’<$MTArchiveTitle dirify=”1″$>’<MTGlue>, </MTGlue></MTArchiveList></MTGlueContainer>);

$count = count($categories);

for($c = 0; $c <= $count; $c++){
if ($thisarch==$categories[$c]) {
//Check to see if this is the first element of the array
if ($c==0) {
$next = intval($c+1);
echo $mainpath, $separator;
echo $file_pre, $categories[$next];
echo $file_suf, $categories[$next], ” ?</a>”;
//no previous
}
//check to see if this is the last element of the array
elseif ($c==$count) {
$next = intval(0);
$prev = intval($c-1);
echo $file_pre, $categories[$prev];
echo $file_suf, “? “, $categories[$prev], “</a>”;
echo $separator, $mainpath;
//no next
}
//all else are normal
else {
$next = ($c+1);
$prev = ($c-1);
echo $file_pre, $categories[$prev];
echo $file_suf, “? “, $categories[$prev], “</a>”;
echo $separator, $mainpath, $separator;
echo $file_pre, $categories[$next];
echo $file_suf, $categories[$next], ” ?</a>”;
}
}
}
?>

Brenna has an alternate version of this script in the original thread - we both were working on it at the same time!!

Post any questions or suggestions in the comments! :)

10 Responses to “next/previous category archive”

  1. kadyellebee Says:
    coding girl!
    I’ve been a busy little coding girl today. Which didn’t necessarily get my to do list caught up on, but

  2. rayne Says:

    After all the fuss, now brad has come out with a plugin [Supplemental Category Tags] to do this exact thing. It’s supposed to be alphabetical but for some reason when I tried it, it wasn’t.

    Also, on the script above, if there is only one category, it still shows the &raquo next to home. maybe a tweak is needed to first check if there is only one category…display only the link to home.

  3. kristine Says:

    Thanks, I saw Brad had come out with a plugin this morning and was coming to post the info here. After Brenna and me played with the PHP, she suggested to Brad that it might be a good thing to have a plugin for. I’m glad I got a chance to learn how to do this, but the plugin is probably easier for most people.

    Oh, and I wouldn’t have assumed anyone with only one category would try to use this script, so that’s why I didn’t write in the code any sort of a test for that :) Don’t really need the extra coding when most people wouldn’t bother installing it w/only one category!! :)

  4. Movable Type -- Kristine's Plugin Directory Says:
    Archive Date Header Plugin
    Used in creation of the Collapsing Archive List.

  5. Movable Type -- Kristine's Plugin Directory Says:
    Glue Plugin
    Used at the bottom of my kadyellebee index page for the last 15 entries, and on my category archives pages

  6. Movable Type -- Kristine's Plugin Directory Says:
    Supplemental Category Tags Plugin
    Can be used on Category archives instead of the PHP option I wrote at ScriptyGoddess.

  7. The After Hours Pub Says:
    Spend a morning in my shoes
    I say, ok, smileys NOW! Gotta start from scratch, so I go over to the MT website to get into the support forums. But I never make it that far. The latest entry on MT is that they’ve been nominated…

  8. mindTangle Says:
    Plugin Time!
    woohoo! found a directory of plugins. here are some that i may want to install, soon: - topicIcons - filterCategories…

  9. Abhimanyu Chirimar Says:

    Amazing!!
    I implemented your script and then used Brad’s. But this is like a godsend. I spent 1 day hacking my head to do this and within minutes I was up. Thank you o goddess

  10. katherine Says:

    Can this code be used with the SubCategories Plugin so that I can do previous/next through the subcategory only?

    Also, I’d like to use it on the main index page as well but I get a build error. Works fine on category archive pages!!