Created
April 30, 2013 00:27
-
-
Save raideus/5485853 to your computer and use it in GitHub Desktop.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?php | |
/** | |
* Returns an array of dates in which content has been published | |
* | |
* @param string $date_type 'day', 'month', or 'year' | |
* @param string $format PHP date format string | |
* @param string $post_type Post type to get dates for | |
* @return array Array of dates in which content has been published | |
*/ | |
function get_dates($date_type = 'year', $post_type = 'post', $format = false ) { | |
$display_format = "Y"; | |
$compare_format = "Y"; | |
if ($date_type == 'month') { | |
$display_format = "M Y"; | |
$compare_format = "Y-m"; | |
} else if ($date_type == 'day') { | |
$display_format = "M j, Y"; | |
$compare_format = "Y-m-d"; | |
} | |
if ($format) $display_format = $format; | |
$posts = get_posts(array('numberposts' => -1, 'post_type' => $post_type)); | |
$previous_display = ""; | |
$previous_value = ""; | |
$count = 0; | |
$dates = array(); | |
foreach($posts as $post) { | |
$post_date = strtotime($post->post_date); | |
$current_display = date_i18n($display_format, $post_date); | |
$current_value = date($compare_format, $post_date); | |
if ($previous_value != $current_value) { | |
$dates[$current_value] = $current_display; | |
} | |
$previous_display = $current_display; | |
$previous_value = $current_value; | |
} | |
return $dates; | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment