scriptygoddess

29 Oct, 2002

next/previous category archive

Posted by: kristine In: MT hacks

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 | rayne

November 2nd, 2002 at 8:29 pm

Avatar

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.

2 | kristine

November 2nd, 2002 at 9:14 pm

Avatar

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!! :)

3 | Abhimanyu Chirimar

July 8th, 2003 at 12:47 am

Avatar

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

4 | katherine

March 4th, 2004 at 3:13 pm

Avatar

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!!

5 | kadyellebee

October 29th, 2002 at 10:20 pm

Avatar

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

6 | Movable Type -- Kristine's Plugin Directory

November 30th, 2002 at 4:04 pm

Avatar

Archive Date Header Plugin
Used in creation of the Collapsing Archive List.

7 | Movable Type -- Kristine's Plugin Directory

November 30th, 2002 at 6:18 pm

Avatar

Glue Plugin
Used at the bottom of my kadyellebee index page for the last 15 entries, and on my category archives pages

8 | Movable Type -- Kristine's Plugin Directory

November 30th, 2002 at 7:14 pm

Avatar

Supplemental Category Tags Plugin
Can be used on Category archives instead of the PHP option I wrote at ScriptyGoddess.

9 | The After Hours Pub

April 9th, 2003 at 2:53 pm

Avatar

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…

10 | mindTangle

April 24th, 2003 at 2:08 pm

Avatar

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

Featured Sponsors

Genesis Framework for WordPress

Advertise Here


  • Scott: Just moved changed the site URL as WP's installed in a subfolder. Cookie clearance worked for me. Thanks!
  • Stephen Lareau: Hi great blog thanks. Just thought I would add that it helps to put target = like this:1-800-555-1212 and
  • Cord Blomquist: Jennifer, you may want to check out tp2wp.com, a new service my company just launched that converts TypePad and Movable Type export files into WordPre

About


Advertisements