17 Apr, 2002
Next/Prev Links Specific to Categories in MT – v.2
Jenn and I spent all day long working on earlier versions of this script, however we believe that this is the quickest, most automated way to do things.
NOTE: This will only work with MT 2.0 + as it relies on the ability to create multiple archive templates.
Step 1: Go to your template section in MT and click "Create new archive template" (near the middle of the page). You can select any Name you'd like. In the template body place the following text exactly as it appears:
<?
$a = 0;
$this_category = "<$MTArchiveCategory dirify="1"$>";
?>
<MTEntries>
<?
if ("<$MTEntryCategory dirify="1"$>"==$this_category) {
$<$MTArchiveCategory dirify="1"$>idarraytemp[$a] = "<$MTEntryID encode_php="qq"$>";
$<$MTArchiveCategory dirify="1"$>linkarraytemp[$a] = "<$MTEntryLink encode_php="qq"$>";
$a++;
}else{
// do nothing
}
?>
</MTEntries>
Save the new template.
Step 2: Go to your Blog Config, click the Archiving link then select the ADD NEW button.
Step 3: Select Archive Type: "CATEGORY" and select the template name you just created in Step 1 under Template. Hit the Add button.
Step 4: You should now see two templates next to the Category Archive Type. Make sure the category box is checked:
You'll notice there's a Category Archive Template with the radio box checked (That's the DEFAULT – you don't want to switch it!) and there's CategoryArray, which is the name of the archive template I created.
In the box next to the name of the archive template you created, place the following:
arrays/<$MTArchiveCategory dirify="1"$>.php
This will create a page for each category with the array we'll need to execute the code. It will place it inside a folder named "arrays" in your archive directory. Your archive directory should already be CHMOD 777.
Hit SAVE.
Step 5: Rebuild your Category Archives Only. Make sure the pages have been created in your archives directory in a folder named arrays.
Step 6: Inside your Individual Archive Template, place the following code where you'd like your previous/next links to appear. I've highly commented this up to give you an idea of what's going on (hopefully I understand it correctly myself)
You only need to change one line here and that's in red:
<?
# If you have no category selected, this will keep
# the script from breaking
$thisCat = "<$MTEntryCategory dirify="1"$>";
if (!$thisCat == "") {
# You want to change this line. Everything before arrays should be
# the path to your archives directory.
include("/home/username/public_html/pathtoarchives/arrays/<$MTEntryCategory dirify="1"$>.php");
# This line sets the variable $i to zero, then counts the total number
# of 'entries' in your category, then makes sure we stop the script
# when the total number of entries has been reached, so it doesn't
# loop and loop and waste CPU cycles.
for ($i =0; $i < count($<$MTEntryCategory dirify="1"$>idarraytemp); $i++) {
$idarraytemp[$i] = $<$MTEntryCategory dirify="1"$>idarraytemp[$i];
$linkarraytemp[$i] = $<$MTEntryCategory dirify="1"$>linkarraytemp[$i];
}
# If you only have one entry in a category, this will prevent
# the script from breaking
if ($idarraytemp >= 1) {
# This reverses the array so the links will appear in the correct order
$idarray = array_reverse($idarraytemp);
$linkarray = array_reverse($linkarraytemp);
# Here's where the HTML comes in. Anything with a print next
# to it can be customized to fit your needs, but be careful or
# you might end up breaking the script.
print("<p>");
# Searches in your array file for the current MTEntry ID in order
# to get the idarray. Then adds 1 to get the next entry idarray
# and subtracts 1 to get the previous entry idarray
$thisKey = array_search("<$MTEntryID$>", $idarray);
$thisKeyNext = $thisKey + 1;
$thisKeyPrevious = $thisKey – 1;
# If there is no previous entry, do nothing. Otherwise
# Print a link to the previous entry. Again, you can
# change this to fit your needs, but be sure you know
# what you're doing.
if ($thisKeyPrevious < 0) {
// do nothing
} else {
print('<a href="');
print($linkarray[$thisKeyPrevious]);
print('">see the previous post in this category</a>');
$previouslinkshown = "true";
}
# Same as with the previous entry.
if ($thisKeyNext >= count($linkarray)) {
// do nothing
} else {
if ($previouslinkshown == "true") {
# Here's the Next/Prev separator. Might want to change it
print(" || ");
}
print('<a href="');
print($linkarray[$thisKeyNext]);
print('">see the next post in this category</a>');
}
print("<br>");
print("This current category is: ");
print("<$MTEntryCategory$>");
print("</p>");
} else {
// do nothing
}
}
?>
All you need to do is replace the line in red with the server path to your archives.
And that's it! Please let us know if you have any questions or run into any glitches with this script.