General
Getting & Giving Support
Administration
Design
- Tut. - Layouts and Themes
- Tut. - Navigation cookbook
- Tut. - How To section
Reference Material
Plugin Development
- Tut. - Writing a plugin
- Ref. - List of Events
- Ref. - Enabling cron jobs
- Ref. - PHPDoc
Core Development
- Ref. - PHPDoc
Press information
Wolf CMS books section
Count of articles-per-month
Introduction
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'); array_push($articleList,$articleAdd); } // 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> <ul> <?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; ?>) </li> <?php endforeach; ?> </ul>
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.
