scriptygoddess

17 Apr, 2002

Next/Prev Links Specific to Categories in MT – v.2

Posted by: Lynda In: How to's|MT hacks

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:

This is what it should look like

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.

64 Responses to "Next/Prev Links Specific to Categories in MT – v.2"

1 | thatkgirl

May 2nd, 2003 at 5:27 pm

Avatar

This might sound like a stupid question, but if you want to do the prev/next by date instead of category, how do you do that (i.e. what codes do you use)?

2 | Jim

May 25th, 2003 at 9:20 pm

Avatar

I'm doing about the same thing as Jael, but I'm using the <MTEntryExcerpt> tag between the <MTEntryPrevious> and the <MTEntryNext> tags. Anyway, I get a parse error (unexpected T_STRING) if there is no previous entry. Every other entry works fine except the first one.

3 | Jael

May 25th, 2003 at 10:06 pm

Avatar

Jim, I didn't get that error, but the setup of using MTEntryMore/MTEntryExcerpt was actually a bit more complicated than the way I posted about it here. It worked fine at first, but some things got mixed up and it didn't work the way I wanted it to.

I got it all going now, so if anyone needs help with it, I'll be glad to send my code to you.

4 | Jim

May 25th, 2003 at 11:15 pm

Avatar

Jael, thanks for the quick response. I'd would greatly appreciate your code so I can see what I'm doing wrong. Thanks again

5 | Jael

May 26th, 2003 at 10:17 am

Avatar

Here is what I really did to use $more, $excerpt, and $title tags in my category-specific templates. You can see an example of what it looks like here. (I took the "Browse all photos" out of this template.)

***********************************
CATEGORY-SPECIFIC BROWSING TEMPLATE
***********************************

<?
$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"$>";
$<$MTArchiveCategory dirify="1"$>morearraytemp[$a] = "<$MTEntryMore encode_php="qq" convert_breaks="0"$>";
$<$MTArchiveCategory dirify="1"$>excerptarraytemp[$a] = "<$MTEntryExcerpt encode_php="qq" convert_breaks="0"$>";
$<$MTArchiveCategory dirify="1"$>titlearraytemp[$a] = "<$MTEntryTitle encode_php="qq"$>";

$a++;
}else{
// do nothing
}
?>
</MTEntries>

>>> NOTES: Added a line for every extra tag I will be using (copy+paste, then replace i.e. "link" wih "excerpt" or "link" with "more").

************************
INDIVIDUAL ENTRY ARCHIVE
************************

<?php
# 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("/FULL/PATH/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];
$morearraytemp[$i] = $<$MTEntryCategory dirify="1"$>morearraytemp[$i];
$excerptarraytemp[$i] = $<$MTEntryCategory dirify="1"$>excerptarraytemp[$i];
$titlearraytemp[$i] = $<$MTEntryCategory dirify="1"$>titlearraytemp[$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);
$morearray = array_reverse($morearraytemp);
$excerptarray = array_reverse($excerptarraytemp);
$titlearray = array_reverse($titlearraytemp);

# 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("<b>Currently browsing: ");
print("<$MTEntryCategory$></b><br /><br />");
print("<table border=\"0\" align="center"><tr><td align=\"center\">");

# 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) {
print('<table border="0"><tr><td align="center"><a href="');
print($linkarray[$thisKeyPrevious]);
print('"><img src="<$MTBlogURL$>/<$MTEntryCategory dirify="1"$>/');
print($morearray[$thisKeyPrevious]);
print('/thumbs/thumb_');
print($excerptarray[$thisKeyPrevious]);
print('.jpg" width="150" height="30" class="thumb" align="middle" alt=""');
print($titlearray[$thisKeyPrevious]);
print('"" /></a><br />« previous</td></tr></table>');
$previouslinkshown = "true";

}

# Same as with the previous entry.
if ($thisKeyNext < count($linkarray)) {
if ($previouslinkshown == "true") {
# Here's the Next/Prev separator. Might want to change it
print("</td><td align=\"center\">  ¦¦  </td><td>");
}
else {
# this is the first entry in the category
}
print('<table border="0"><tr><td align="center"><a href="');
print($linkarray[$thisKeyNext]);
print('"><img src="<$MTBlogURL$>/<$MTEntryCategory dirify="1"$>/');
print($morearray[$thisKeyNext]);
print('/thumbs/thumb_');
print($excerptarray[$thisKeyNext]);
print('.jpg" width="150" height="30" class="thumb" align="middle" alt=""');
print($titlearray[$thisKeyNext]);
print('"" /></a><br />next »</td></tr></table>');
}
}
print("</td></tr></table>");

}
?>

>>> NOTES:
– The first two sets of bold lines are where you need to add other tags you will be using (i.e. $more, $excerpt, etc.). If you are not using $excerpt for instance, delete all those related lines.
– The last two sets of bold lines are the previous/next sections. The parts that will be most important to you are the ones in italic. That's where everything will be written out that is contained in those tags.
– Since I'm using this for a photo section, it might look a little confusing with all the extra image-related tags. If I tried to remove something, I'd probably mess up the code, so I won't. Besides, it might be helpful to other photo section users. You can take any print(""); lines out that you won't need (i.e. print('/thumbs/thumb_');).
– I inserted a table spread out within the code, so if you don't need that, delete all table, tr, and td tags. If you miss one, your layout will probably look messed up. Or you can keep it, it's to align the next/previous/current tags right.

*******************************************
INDIVIDUAL ENTRY ARCHIVE – ACTUAL IMAGE TAG (just for reference)
*******************************************

<img src="<$MTEntryExcerpt convert_breaks="0"$>.jpg" alt="*<$MTEntryTitle$>*" class="image" border="1"><br /><br />
<$MTEntryBody convert_breaks="0"$>

That's that. If something looks odd, double-check that you didn't miss any < > tags. If something isn't clear, I'm around, just ask. Good luck!

6 | Jim

May 26th, 2003 at 1:17 pm

Avatar

Jael, thanks again. I was afriad that I've have to do something like that. BTW, your site has a great design.

7 | Jai

June 6th, 2003 at 3:52 pm

Avatar

Anybody know of a way to do this in ASP rather than PHP? It looks similar, but some of my templates use ASP variables. I guess I could learn PHP, but I'd rather be lazy and have someone who understands teh script above tell me what the ASP equivalents to the PHP arguments are. I'll rewrite the script if I know that correlation and post it.

Nice scripting ladies…

8 | Jai

June 6th, 2003 at 5:44 pm

Avatar

Nevermind my last comment… I learned PHP in about an hour… well, all that I needed to know of it… It's almost identical to ASP in structure, so it was easy to turn my site into PHP…

http://www.jaiandbecky.com/bg/

And the script is great, thanks Scripties!

9 | Patrick

July 2nd, 2003 at 10:48 pm

Avatar

I haven't yet tried this out, but I want to know if it could work with entries that are assigned to multiple categories. If so, how? Would the Indiv Arch Page have to have the prev and next links for all categories that the entry is in? How might that work? Or won't it?

10 | Andrew

August 13th, 2003 at 9:52 pm

Avatar

Has anyone taken this prev/next within a category idea and made a plugin? I'd rather not bother with php on the site I want to try it on.

11 | Becky

August 16th, 2003 at 12:58 pm

Avatar

I have a question about using this on my photoblog. I've read through all the comments but haven't been able to assess if I can modify this to work with my blog.

My photo blog incorporates two types of entries; GENERAL and PHOTOS. General entries are only text/blog entries and the MT code causes only those entries to show up in a certain spot.

PHOTOS are all added and placed in the PHOTOS category. I have then added additional categories in the blog and assigned the photos MULTIPLE categories. That's how I get just the PLANTS or CREATURES to show up on a single archive page alone.

But by setting up my blog this way when I attempt to implement this script it only sees the MAIN "photos" category".

Is there a way to make this work with my current blog setup?

Thank you in advance for any help. I'll continue to "play" with it on my own but I haven't come up with a simple way to integrate it into the "multiple" categories as of yet.

Url of photoblog: http://digitalasylum.net/vieus/

12 | tommo

December 2nd, 2003 at 5:22 pm

Avatar

thanks for the great code. only one problem, the "next" button actually goes to a the previous photo and vice-versa. how can i change the code to fix this. i dont want to change the order in the mt config because i need it to be ascending for the thumnails page.

cheers
tom

13 | Tobias

January 2nd, 2004 at 6:42 am

Avatar

Hi Lynda and Jenn,

thx for the great script, which I got immediatly working. But then I changed my blog with the plugin "dirifyplus" and I got the following problem (codes underneath):

Error message in the "include" line:

Parse error: parse error, unexpected '[' in /srv/www/htdocs/www.xento.net/wir/unsere-vergangenheit/neuseeland/cape-reinga/noch-ein-xenia-bild.php on line 57

"Array"-Template

<?
$a = 0;
$this_category = "<$MTArchiveCategory dirifyplus="sld"$>";
?>
<MTEntries>
<?
if ("<$MTEntryCategory dirifyplus="sld"$>"==$this_category) {
$<$MTArchiveCategory dirifyplus="sld"$>idarraytemp[$a] = "<$MTEntryID encode_php="qq"$>";
$<$MTArchiveCategory dirifyplus="sld"$>linkarraytemp[$a] = "<$MTEntryLink encode_php="qq"$>";
$a++;
}else{
// do nothing
}
?>
</MTEntries>

Individual-Template (php only, comments removed)

<?

$thisCat = "<$MTEntryCategory dirifyplus="sld"$>";
if (!$thisCat == "") {

include("/srv/www/htdocs/www.xento.net/wir/unsere-vergangenheit/neuseeland/arrays/<$MTEntryCategory dirifyplus="sld"$>.php");

for ($i =0; $i < count($<$MTEntryCategory dirifyplus="sld"$>idarraytemp); $i++) {
$idarraytemp[$i] = $<$MTEntryCategory dirifyplus="sld"$>idarraytemp[$i];
$linkarraytemp[$i] = $<$MTEntryCategory dirifyplus="sld"$>linkarraytemp[$i];
}

if ($idarraytemp >= 1) {

$idarray = array_reverse($idarraytemp);
$linkarray = array_reverse($linkarraytemp);

print("<p>");

$thisKey = array_search("<$MTEntryID$>", $idarray);
$thisKeyNext = $thisKey + 1;
$thisKeyPrevious = $thisKey – 1;

if ($thisKeyPrevious < 0) {
// do nothing
} else {
print('<a href="');
print($linkarray[$thisKeyPrevious]);
print('">see the previous post in this category</a>');
$previouslinkshown = "true";
}

if ($thisKeyNext >= count($linkarray)) {
// do 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>");
} else {
// do nothing
}
}

Do you have any idea, what I'm doing wrong? You can see the "live" result here: Neuseeland 1999

Thanks in advance,

Tobias

14 | Neil

July 31st, 2004 at 7:06 pm

Avatar

I am building my site as explained by Brad Choate, 'Doing your whole site with MT' and would like to know if this script will work with Brad's setup?

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