scriptygoddess

19 Nov, 2002

6 months of archives

Posted by: Jennifer In: MT hacks

Heather asked me if it were possible to make your archives listing only show the last 6 months one way, and then the rest another way.

She wanted the last 6 months of archives to display like:

November 2002
02 Nov 19: post title
02 Nov 02: post title

October 2002
02 Oct 30: post title…

and then the rest of the archives just in month format like this:

April 2002
March 2002

At first I would have said to do something with <MTArchiveList archive_type-"monthly" offset="6"> but it that "offset" attribute doesn't seem to get applied like that (it only offsets specific ENTRIES, not archive types). So I made a very (kind of scary) script to do exactly what she wanted. There may be some shortcuts I've overlooked, so please let me know if you think there's an easier way.

(This would go in an index-template, where you want your archives to display like this…)

<?
//JUST CHANGE THE VARIABLES BELOW
$db_server = "localhost"; //probably don't need to change this
$db_name = "mt-database-name";
$db_username = "db-username";
$db_password = "db-password";
$pathtoarchives = "http://www.YOURDOMAIN.com/archives/";
$extname = ".php";
$blogid = "1"; //what's your mt blog id for this blog?
///////////////////////////////////////////////////
//connect to database
//get 6mos ago
$today = getdate();
$this_month = $today['mon'];
$this_day = $today['mday'];
$this_year = $today['year'];
if ($this_month == "1") {
$last_month = "7";
$this_year = $this_year-1;
} else if ($this_month == "2") {
$last_month = "8";
$this_year = $this_year-1;
} else if ($this_month == "3") {
$last_month = "9";
$this_year = $this_year-1;
} else if ($this_month == "4") {
$last_month = "10";
$this_year = $this_year-1;
}else if ($this_month == "5") {
$last_month = "11";
$this_year = $this_year-1;
}else if ($this_month == "6") {
$last_month = "12";
$this_year = $this_year-1;
} else {
$last_month = $this_month-6;
}
if ($this_day < 10) {
$this_day = "0".$this_day;
}
if ($last_month < 10) {
$last_month = "0".$last_month;
}
$sixmosago = $this_year."-".$last_month."-01 00:00:00";
$db = mysql_pconnect("$db_server","$db_username","$db_password")
or die("Could not connect");
mysql_select_db("$db_name");
$queryA = sprintf("SELECT entry_id, entry_title, entry_created_on, entry_blog_id FROM mt_entry WHERE entry_blog_id='%s' and entry_created_on > '%s' ORDER BY entry_created_on DESC", $blogid, $sixmosago);
$resultA = mysql_query($queryA);
$numresults = mysql_num_rows($resultA);
for ($i = 0; $i <$numresults; $i++) {
$row = mysql_fetch_array($resultA);
$entrydate = getdate(strtotime($row['entry_created_on']));
$entrymonth = substr ($entrydate['month'], 0,3);
if ($entrydate['mday'] < 10) {
$entryday = "0".$entrydate['mday'];
} else {
$entryday = $entrydate['mday'];
}
$entryyear = substr ($entrydate['year'], 2,2);

if (!isset($lastround)) {
$lastround = "";
}
if ($lastround != $entrymonth) {
echo "<br>".$entrydate['month']." ".$entrydate['year']."<br>";
}
echo $entryyear." ".$entrymonth." ".$entryday.": ";
echo "<a href='".$pathtoarchives.str_pad($row ['entry_id'],6,"0",STR_PAD_LEFT).$extname."'>".stripslashes($row['entry_title'])."</a>";
echo "<br>";
$lastround = $entrymonth;
}
mysql_close($db);
?>
<br>
<MTArchiveList archive_type="Monthly">
<?
if (strtotime("<$MTArchiveDate format="%B %e, %Y %I:%M %p"$>") >strtotime($sixmosago)) {
// do nothing
} else {
$checkdouble = $entrydate['month']." ".$entrydate['year'];
if ($checkdouble == "<$MTArchiveDate format="%B %Y"$>") {
//do nothing
} else {
?>
<a href="<$MTArchiveLink$>"><$MTArchiveDate format="%B %Y"$></a><br>
<?
}
}
?>
</MTArchiveList>

4 Responses to "6 months of archives"

1 | kristine

November 19th, 2002 at 8:21 pm

Avatar

Very Cool :) Just in case this is helpful tooo, I have a contracting archives (all php) that could do something similar (but not nearly as cool and complex as the above!) with monthly archives if you wanted all of the archives there, but just to hide and show on command.

2 | heather

November 21st, 2002 at 4:30 pm

Avatar

Jenn, you just rock! In case anyone wants to see the finished product in use over at my site —> here :-) Thanks a bunch!!

3 | dave

December 2nd, 2002 at 1:22 pm

Avatar

Hello. I am thinking about using your script in my MT blog, but I was wondering about only displaying the last 3 months instead of 6 or 7. I don't want to chop and mess up the script too much….but it looks easy enough to change.
Thanks for your time and help.

4 | Tara

May 19th, 2003 at 11:37 am

Avatar

The way you've written this script, the user needs to be using MySQL for MT. Is there any way to use it with a Berkeley database?

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