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

The_Source-1’s picture

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

The_Source-1’s picture

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.

// This template page is for showing blog archives.
//show blog archives from 2007 - 2099 with most recent dates shown first
for($y = 99; $y >= 7; $y--)
{
$showYear = false;
for($m = 12; $m >= 1; $m--)
{
if (blogList($m,$y))
$showYear = true;
}
if ($showYear)
{
echo '<h2>'. (2000 + $y) .'</h2>';
for($m = 12; $m >= 1; $m--)
{
echo blogList($m,$y);
}
}
}

//blog archives
function blogList($month, $year)
{
$lastDate = array(31,29,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"');

//The_Source: count the number of blogs here for v6.0
//There is probably a better way to do this

$num_blog_nodes = db_result(db_query('SELECT COUNT(*) 
FROM {node} 
WHERE created > '.$beginDate. ' AND created < '.$endDate .' AND type = "blog"'));

if($num_blog_nodes > 0)
{
$output =  '<h3>'.date('F', $beginDate) .'</h3><ul>';
while($blog = db_fetch_object($query))
{
$output .= '<li>'.l($blog->title, 'node/'.$blog->nid) .'</li>';
}
$output .= '</ul>';
return $output;
}
else
{
return false;
}
}
mindshare’s picture

Hi, I'm using your updated code but am getting this error:

user warning: You have an error in your SQL syntax; check the manual that corresponds to your MySQL server version for the right syntax to use near 'AND created < AND type = "blog"' at line 1 query: SELECT nid, title, created, type FROM node WHERE created > AND created < AND type = "blog" in /home/sites/all/themes/mytheme/blog-archive.php on line 31.
user warning: You have an error in your SQL syntax; check the manual that corresponds to your MySQL server version for the right syntax to use near 'AND created < AND type = "blog"' at line 1 query: SELECT COUNT(*) FROM node WHERE created > AND created < AND type = "blog" in /home/sites/all/themes/mytheme/blog-archive.php on line 36.

Any suggestions?

ka0osk’s picture

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

ka0osk’s picture

// This template page is for showing blog archives.
//show blog archives from 2007 - 2099 with most recent dates shown first
for($y = 25; $y >= 7; $y--)
{
$showYear = false;
for($m = 12; $m >= 1; $m--)
{
if (blogList($m,$y))
$showYear = true;
}
if ($showYear)
{
echo '<h2>'. (2000 + $y) .'</h2>';
for($m = 12; $m >= 1; $m--)
{
echo blogList($m,$y);
}
}
}

//blog archives
function blogList($month, $year)
{
$lastDate = array(31,29,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-1], $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"');

//The_Source: count the number of blogs here for v6.0
//There is probably a better way to do this

$num_blog_nodes = db_result(db_query('SELECT COUNT(*)
FROM {node}
WHERE created > '.$beginDate. ' AND created < '.$endDate .' AND type = "blog"'));

if($num_blog_nodes > 0)
{
$output =  '<h3>'.date('F', $beginDate) .'</h3><ul>';
while($blog = db_fetch_object($query))
{
$output .= '<li>'.l($blog->title, 'node/'.$blog->nid) .'</li>';
}
$output .= '</ul>';
return $output;
}
else
{
return false;
}
}

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

asanchez75’s picture

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


<?php

drupal_add_js('(function($) {
  $.fn.kshide = function() {
    
    return this.each(function() {
        var obj = $(this);
        var link = obj.children("a");
        var list = obj.children("ul");
        list.hide();
        
        link.toggle(
                function() {
                    $(list).show("fast");
                },
                function() {
                    $(list).hide("fast");
                });
    });
  }
})(jQuery);','inline');


drupal_add_js('$(document).ready(function(){$(".tog").kshide();});','inline');
?>

<?php

// This template page is for showing blog archives.
//show blog archives from 2007 - 2099 with most recent dates shown first

echo '<ul class="menu">';

for($y = 25; $y >= 7; $y--)
{
$showYear = false;
for($m = 12; $m >= 1; $m--)
{
if (blogList($m,$y))
$showYear = true;
}
if ($showYear)
{
echo '<li class="tog"><a href="#"><h2>'. (2000 + $y) .'</h2></a>';

for($m = 12; $m >= 1; $m--)

{
echo blogList($m,$y);

}
echo '</li>';
}
}

echo '</ul>';

//blog archives
function blogList($month, $year)
{
$lastDate = array(31,29,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-1], $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 = "bitacora"');

//The_Source: count the number of blogs here for v6.0
//There is probably a better way to do this

$num_blog_nodes = db_result(db_query('SELECT COUNT(*)
FROM {node}
WHERE created > '.$beginDate. ' AND created < '.$endDate .' AND type = "bitacora"'));

if($num_blog_nodes > 0)
{
$output =  '<ul><li class="tog"><a href="#"><h3>'.date('F', $beginDate) .'</h3></a><ul>';
while($blog = db_fetch_object($query))
{
$output .= '<li>'.l($blog->title, 'node/'.$blog->nid) .'</li>';
}
$output .= '</ul></li></ul>';
return $output;
}
else
{
return false;
}
}

?>

jamesmcd’s picture

Hello,

Did you just create a new block and drop the code in?

Many thanks

shermang’s picture

I made 1 fix to the above code so that it selects blog instead of bitacora.

Also, I added counts to the months.

<?php

    drupal_add_js('(function($) {
    $.fn.kshide = function() {

    return this.each(function() {
    var obj = $(this);
    var link = obj.children("a");
    var list = obj.children("ul");
    list.hide();

    link.toggle(
    function() {
    $(list).show("fast");
    },
    function() {
    $(list).hide("fast");
    });
    });
    }
    })(jQuery);','inline');


    drupal_add_js('$(document).ready(function(){$(".tog").kshide();});','inline');

    // This template page is for showing blog archives.
    //show blog archives from 2007 - 2099 with most recent dates shown first

    echo '<ul class="menu">';

    for($y = 25; $y >= 7; $y--)
    {
        $showYear = false;
        for($m = 12; $m >= 1; $m--)
        {
            if (blogList($m,$y))
                $showYear = true;
        }
        if ($showYear)
        {
            echo '<li class="tog"><a href="#"><h2>'. (2000 + $y) .'</h2></a>';

            for($m = 12; $m >= 1; $m--)

            {
                echo blogList($m,$y);

            }
            echo '</li>';
        }
    }

    echo '</ul>';

    //blog archives
    function blogList($month, $year)
    {
        $lastDate = array(31,29,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-1], $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"');

        //The_Source: count the number of blogs here for v6.0
        //There is probably a better way to do this

        $num_blog_nodes = db_result(db_query('SELECT COUNT(*)
        FROM {node}
        WHERE created > '.$beginDate. ' AND created < '.$endDate .' AND type = "blog"'));

        if($num_blog_nodes > 0)
        {
            $output =  '<ul class="tog"><a href="#"><h3>'.date('F', $beginDate) . '('.$num_blog_nodes.')</h3></a><ul>';
            while($blog = db_fetch_object($query))
            {
                $output .= '<li>'.l($blog->title, 'node/'.$blog->nid) .'</li>';
            }
            $output .= '</ul></ul>';
            return $output;
        }
        else
        {
            return false;
        }
    }
?>
makcum’s picture

Well, archive.module worked for me just fine.

http://www.blogfortrader.com/archive

Cheers

LuisSanchez’s picture

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