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]

Posts Date older than, or not older than given date

With this small ShortCode you can compare the Views Post Date (returning “U” Timestamp) to any date in past or future.

It’s useful as example if you want to display a “NEW” Tag on posts that are not older than [given date or days]

Function:

function timestamp_in_past_shortcode($atts) {
$timestamp_in_past = strtotime('-1 Week');//here you can use any value
return $timestamp;
}
add_shortcode('date-in-past', 'timestamp_in_past_shortcode');

Then evaluate the above ShortCode against the Views ShortCode

1453802101