Group Posts by Date (year and month)

This ShortCode let’s you “group” posts in a View by their Publish Date, by year and then month.

To use it, add this to functions.php:

 

add_shortcode('heading', 'my_heading');
function my_heading($atts, $content = '') {
  static $year = null;
  static $month = null;
  $condition = $atts['condition'];
  $value = $atts['value'];
  switch ($condition) {
      case 'year':
      case 'month':
        if ($$condition != $value) {
            $$condition = $value;
            return $content;
        }
        break;
  }
  return '';
}

Then use this new ShortCode in the Views loop as the below example:

[heading condition="year" value="2016"]
<h4>2016</h4>
[/heading]
[heading condition="month" value="June"]
<h5>June</h5>
[/heading]