17 Apr, 2002
How to set up next/previous links specific to categories in MT…
Posted by: Jennifer In: How to's
There's quite a bit of upfront work, but if you really want this… then maybe it'd be worth it to you. You'll also need to understand a *little* php. I'll try to explain it the best I can, if you have any questions, let me know, be as specfic as you possibly can, and hopefully I (or one of the other scriptygoddesses) can help…
(MAJOR MAJOR Thanks to goddessLynda for her help on this one!)
(UPDATE: She just doesn't give up either! LOL! Follow along – but DO look at the comments – Lynda posted a comment with a better/easier way than mine…)
(MORE UPDATE: Lynda and I are still working on this… we're trying to make it as automated as possible so it's not so difficult to implement… and also make it as more friendly to the server.. less work – you might want to wait for version 2 of this tutorial – after we've had a chance to get this working…)
LAST AND FINAL UPDATE: I'm leaving this post here more for my own information – but Lynda wrote up the tutorial for the revised way to do this – which summarizes all the commenting back and forth. It's almost completely plug-n-play. Go HERE for the best method.
First you'll need to create a new index template in MT. I named my categoryArrays.php This file will contain an array of all your posts (one array per category). Here's what mine looks like:
<?
$i = 0;
$j = 0;
$k =0;
$l = 0;
$m = 0;
?>
<MTArchiveList>
<MTEntries>
<?
if ("<$MTEntryCategory$>"=="how to's") {
$howtoidarraytemp[$i] = "<$MTEntryID encode_php="qq"$>";
$howtolinkarraytemp[$i] = "<$MTEntryLink encode_php="qq"$>";
$i++;
} else if ("<$MTEntryCategory$>"=="MT hacks") {
$mthacksidarraytemp[$j] = "<$MTEntryID encode_php="qq"$>";
$mthackslinkarraytemp[$j] = "<$MTEntryLink encode_php="qq"$>";
$j++;
} else if ("<$MTEntryCategory$>"=="bookmarks") {
$bookmarksidarraytemp[$k] = "<$MTEntryID encode_php="qq"$>";
$bookmarkslinkarraytemp[$k] = "<$MTEntryLink encode_php="qq"$>";
$k++;
} else if ("<$MTEntryCategory$>"=="lessons learned") {
$lessonsidarraytemp[$l] = "<$MTEntryID encode_php="qq"$>";
$lessonslinkarraytemp[$l] = "<$MTEntryLink encode_php="qq"$>";
$l++;
} else if ("<$MTEntryCategory$>"=="suggested reading") {
$readingidarraytemp[$m] = "<$MTEntryID encode_php="qq"$>";
$readinglinkarraytemp[$m] = "<$MTEntryLink encode_php="qq"$>";
$m++;
}
?>
</MTEntries>
</MTArchiveList>
ok – don't panic… let's break that down.
$i = 0;
$j = 0;
$k =0;
etc.
$i that's how you declare a variable in php. I'm initializing them ("= 0") to zero. I declare and initialize a different variable. One for each of my categories.
if ("<$MTEntryCategory$>"=="how to's") {
"how to's" is the name of one of my categories. So you need to set an if (or else if) block to check the name for each of your categories.
$howtoidarraytemp[$i] = "<$MTEntryID encode_php="qq"$>";
$howtolinkarraytemp[$i] = "<$MTEntryLink encode_php="qq"$>";
I'm setting up an array for this category – one for the ID's and one for the links.
$i++;
not a big thing going on here… just adding 1 to that "$i" variable. Just remember to change this to a different letter (matching the letters you declared and initialized above) for each category.
you'll need to repeat that whole mess with each category.
Make sure you:
1) have different names for the id array and the link array in each category
2) change the variable you're using in the [ ] for each category. (in my file, I'm using $i for the how to's, $j for the mt hacks, $k for the bookmarks, etc. etc.)
—————
With me so far? No? HA! Just wait… the fun has only begun! Now lets move on to the individual entries archive…
—————
Here's what I have there:
<?
include("/home/youraccount/public_html/PathToYourFile/categoryArrays.php");
if ("<$MTEntryCategory$>"=="how to's") {
for ($i =0; $i < count($howtoidarraytemp); $i++) {
$idarraytemp[$i] = $howtoidarraytemp[$i];
$linkarraytemp[$i] = $howtolinkarraytemp[$i];
}
} else if ("<$MTEntryCategory$>"=="MT hacks") {
for ($i =0; $i < count($mthacksidarraytemp); $i++) {
$idarraytemp[$i] = $mthacksidarraytemp[$i];
$linkarraytemp[$i] = $mthackslinkarraytemp[$i];
}
} else if ("<$MTEntryCategory$>"=="bookmarks") {
for ($i =0; $i < count($bookmarksidarraytemp); $i++) {
$idarraytemp[$i] = $bookmarksidarraytemp[$i];
$linkarraytemp[$i] = $bookmarkslinkarraytemp[$i];
}
} else if ("<$MTEntryCategory$>"=="lessons learned") {
for ($i =0; $i < count($lessonsidarraytemp); $i++) {
$idarraytemp[$i] = $lessonsidarraytemp[$i];
$linkarraytemp[$i] = $lessonslinkarraytemp[$i];
}
} else if ("<$MTEntryCategory$>"=="suggested reading") {
for ($i =0; $i < count($readingidarraytemp); $i++) {
$idarraytemp[$i] = $readingidarraytemp[$i];
$linkarraytemp[$i] = $readinglinkarraytemp[$i];
}
} else {
$abort = "true";
}
if (!$abort=="true") {
$idarray = array_reverse($idarraytemp);
$linkarray = array_reverse($linkarraytemp);
print("<p>");
$thisKey = array_search("<$MTEntryID$>", $idarray);
$thisKeyNext = $thisKey + 1;
$thisKeyPrevious = $thisKey – 1;
if ($thisKeyPrevious < 0) {
//nothing
} else {
print('<a href="');
print($linkarray[$thisKeyPrevious]);
print('">see the previous post in this category</a>');
$previouslinkshown = "true";
}
if ($thisKeyNext >= count($linkarray)) {
//nothing
} else {
if ($previouslinkshown == "true") {
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>");
}
?>
Ok! Calm down! Breathe… we can do this! (LOL!)
include("/home/youraccount/public_html/PathToYourFile/categoryArrays.php");
That's just to pull in that index file we created up above. Just make sure you have the right SERVER path there…
if ("<$MTEntryCategory$>"=="how to's") {
yup, here we go again… gotta do these blocks for each category
for ($i =0; $i < count($howtoidarraytemp); $i++) {
$idarraytemp[$i] = $howtoidarraytemp[$i];
$linkarraytemp[$i] = $howtolinkarraytemp[$i];
}
ok, what I'm doing here is transfering the contents into another array. For each category – these names are the same ($idarraytemp, and $linkarraytemp) – believe it or not, this is to make things easier later… the $howtoidarraytemp, and the $howtolinkarraytemp – get the names of these arrays from your category array file… the one you created above. The "$i" here doesn't matter… you can just use "$i" in all the [ ].
} else {
$abort = "true";
}
end that block just like that… don't need to change anything there.
believe it or not.. you're basically done… the rest is copy and paste exactly as what I have. (See! Told you I was trying to make things easy on you)
so copy and paste this part as is:
if (!$abort=="true") {
$idarray = array_reverse($idarraytemp);
$linkarray = array_reverse($linkarraytemp);
print("<p>");
$thisKey = array_search("<$MTEntryID$>", $idarray);
$thisKeyNext = $thisKey + 1;
$thisKeyPrevious = $thisKey – 1;
if ($thisKeyPrevious < 0) {
//nothing
} else {
print('<a href="');
print($linkarray[$thisKeyPrevious]);
print('">see the previous post in this category</a>');
$previouslinkshown = "true";
}
if ($thisKeyNext >= count($linkarray)) {
//nothing
} else {
if ($previouslinkshown == "true") {
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>");
}
somewhat a pain in the butt, I know… but no pain, no gain, right?