Complete and flexible blogging platform for ProcessWire

Get Archives

The method getArchives() returns a multidimensional array of Blog Archives, optionally by year and optionally including a few Post titles. The method is not really meant to be used directly on its own but rather in conjunction with its sister method renderArchives() which we will look at in the next lesson. In itself, getArchives() does not output any markup but acts as a helper method to renderArchives(). Hence, we will only briefly look at getArchives() for completeness before we move on to the next lesson where I will show you how to render your Blog Archives using these two methods.

<?php
 /**
 * Get an array of archives, optionally by year and optionally including a few posts.
 *
 * @access public
 * @param int $year Retrieve archives for a specific year (default=retrieve all years)
 * @param int $limit Max number of posts titles to show for each month, default=0 which means don't show any
 * @return $years array()
 *
 */
 public function getArchives($year = 0, $limit = 0)

As seen from the above code, getArchives() accepts two arguments, $year and $limit both of which default to 0. 

The first argument $year accepts a year formatted as an integer, e.g. '2005'. That will instruct getArchives() to retrieve an array of Blog Post Archives for 2005 only. The default value is 0, meaning retrieve archives for all years available. 

The second argument $limit accepts integer values and will control the maximum number of Post titles to retrieve for each month returned in addition to the normal output of month name and number of posts for that month. The default is 0, meaning do not grab any Post titles with the returned Archives.

getArchives() returns an array in the following fomat.

<?php
array(
  2012 => array( // year 2012
    1 => array( // month 1: January
    'name' => 'January', 
    'url' => '/archives/2012/01/',
    'posts' => PageArray, // containing first few posts
    'total' => 5 // total # of posts in month
    ),
    2 => array( // Month 2: February
    'name' => 'February',
    //...and so on
    ),
    //...and so on
  ),

  2011 => array(), // year 2011
  //...and so on
);

The multidimensional array returned by getArchives() is used by renderArchives() to render the Blog Archives.

Summary

This short lesson introduced you to the method getArchives( $year = 0, $limit = 0), a method that gets an array of Blog Archives which in turn are output using its sister method renderArchives(). Hence, getArchives() itself does not output anything but is a helper method for renderArchives().