Light-weight, fast, simple and powerful!
Translations of this page?:

Count of articles-per-month


Wolf comes “out-of-the-box” with a monthly archive for its Articles (“blogging”) section. Many people like to show how many articles have been published in any given month, like this, for example:

  • June 2010 (7)
  • May 2010 (12)
  • April 2010 (9)

Here's some code to achieve that result. It assumes the same page names as Wolf uses by default, so if you have changed “Articles” to “News” or “Blog”, you will need to make an (obvious) change.

Complile utility list for counting

In the Articles sidebar, where the monthly archive listing is found, include this code near the top of the text edit area:

<?php $allArticles = $this->find('articles')->children();
  $articleList = array();
  foreach ($allArticles as $articleEach) {
    $articleAdd = $articleEach->date('%Y/%m','published');
  } // foreach

This creates an array with an entry for each published article, with the date format used by the Month Archive code, ready for later use.

If you are using “News” or “Blog”, rather than “Articles”, then change the first line to use the slug matching your page name.

Creating the Monthly total

Then, replace the “Archives by Month” with the following:

<h3>Archives By Month</h3>
<?php foreach ($archives as $date): ?>
<li><a href="<?php echo BASE_URL . $this->url .'/'. $date . URL_SUFFIX; ?>"><?php echo strftime('%B %Y', strtotime(strtr($date, '/', '-'))); ?></a>
    (<?php $i = 0;
    foreach($articleList as $val) {
    $date == $val ? $i++ : null;
    } echo $i; ?>)
<?php endforeach; ?>

All we have added is the indented section: it uses the array we created in the first step, counting the matches for each month listed, and then displaying the total.

Save the page. The Articles page sidebar should now display the monthly totals in the format shown in the introduction, above.

tutorials/count_monthly_articles.txt · Last modified: 2011-09-12 00:46 (external edit)
Except where otherwise noted, content on this wiki is licensed under the following license:GNU Free Documentation License 1.2
Copyright 2010 / design by yello studio / Wolf CMS Inside