This can probably be wrapped into a small module, or a custom block, but here's what I did to create the blog archives page seen here:
http://intersectionsinternational.org/blog-archives
These steps assume that you have the blog module installed, and that you have some blogs created.
Step 1. Create a new page. - make note of the node. For the example's sake we'll say it's node 43. We don't need any page content, just a title will do fine ("Blog Archives" might be a good choice).
Step 2. Go into your themes folder and copy "page.tpl.php" to make a new file named "page-node-43.tpl.php". Of course, you would put your node number there in place of 43. This is how you can customize a node individually. This should be an exact copy of your page.tpl.php file at this point.
Step 3. Copy the code below into the file page-node-43.tpl.php, below the php line that prints the content (print $content
) :
// This template page is for showing blog archives.
//show blog archives from 2007 - 2019 with most recent dates shown first
for($y = 20; $y >= 7; $y--)
{
$showYear = false;
for($m = 12; $m >= 1; $m--)
{
if (blogList($m,$y))
$showYear = true;
}
if ($showYear)
{
$top = $y == 20 ? 'margin-top:50px;': '';
echo '<h1 style="'.$top.'margin-bottom:6px;">'. (2000 + $y) .'</h1>';
for($m = 12; $m >= 1; $m--)
{
echo blogList($m,$y);
}
}
}
//blog archives
function blogList($month, $year)
{
$lastDate = array(31,28,31,30,31,30,31,31,30,31,30,31);
$beginDate = mkTime(0,0,0, $month, 1, $year);
$endDate = mkTime(0,0,0, $month, $lastDate[$month], $year);
//pull all the blogs where month =
$query = db_query('SELECT nid, title, created, type
FROM {node}
WHERE created > '.$beginDate. ' AND created < '.$endDate .'
AND type = "blog"');
if(db_num_rows($query) > 0)
{
$output = '<p style="margin:0px; padding:0px;">'.date('F', $beginDate) .'</p><ul>';
while($blog = db_fetch_object($query))
{
$output .= '<li>'.l($blog->title, 'node/'.$blog->nid) .'</li>';
}
$output .= '</ul>';
return $output;
}
else
{
return false;
}
}
Comments
Needs to be patched for v6.0
G'day
This needs to be fixed to remove the fatal error caused by the call for the db_num_rows function as it was removed from 6.x core. Please refer here for further information:
http://drupal.org/node/222388?mode=2&sort=2
Self-fix
For v6.0 I went straight for inserting PHP code to the page content. That way there was less stylesheet quirks to handle and I could strip-out hard-coded formatting.
Error in Drupal 6
Hi, I'm using your updated code but am getting this error:
Any suggestions?
Shouldn't that be: $endDate =
Shouldn't that be:
$endDate = mkTime(0,0,0, $month, $lastDate[$month-1], $year);
[$month-1] instead of [$month] ?
and if you go to 99 it will probably gak the mktime function, so I would try something like 25 instead of 99 for the end date. It would be faster anyway, right? I bet that is what is killing the SQL
John Stephenson
// This template page is for
Here is the code with those 2 changes.....This seems to work.
PHP arrays start with 0, not 1!
also...
It would probably be better to use 2025 instead of just 25 for the year, and remove the 2000 being added to y$, just to be less ambiguous. IMHO
John Stephenson
using jQuery to make it similar to block blogspot
Hi,
I used this code thanks to my friend Edu and merge it with this jquery code that I found in
http://www.puttputt.ca/?p=528
The result is similar to archive block of blogspot
Regards,
Adam
Hello, Did you just create a
Hello,
Did you just create a new block and drop the code in?
Many thanks
A fix and a Change
I made 1 fix to the above code so that it selects blog instead of bitacora.
Also, I added counts to the months.
Well, archive.module worked
Well, archive.module worked for me just fine.
http://www.blogfortrader.com/archive
Cheers
I developed your code in a
I developed your code in a easy and independent module. I will upload this. If you want it now, please visit my homepage (www.luis-san.com) and contact me!.
luis