Created
July 7, 2012 11:34
-
-
Save feedmeastraycat/3065976 to your computer and use it in GitHub Desktop.
WordPress: Display archive links based on type and format. Uses the built in wp_get_archives() but with added filters to display archive only for a specific year, month or day
This file contains 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 | |
/** | |
* Display archive links based on type and format. Uses the built in wp_get_archives() but with | |
* added filters to display archive only for a specific year, month or day | |
* @param string|array $args Optional. Override defaults. | |
* @param null|int $year Specify year for archive | |
* @param null|int $month Specify month for archive | |
* @param null|int $day Specify day for archive | |
* @return string|void Return or echo depending on "echo" argument | |
* @author David Mårtensson <[email protected]> | |
*/ | |
function wp_get_archives_specific($args='', $year=null, $month=null, $day=null) { | |
$default = array( | |
'_specific_year' => null, | |
'_specific_month' => null, | |
'_specific_day' => null | |
); | |
$r = wp_parse_args($args, $default); | |
if (!is_null($year)) { | |
$r['_specific_year'] = $year; | |
} | |
if (!is_null($month)) { | |
$r['_specific_month'] = $month; | |
} | |
if (!is_null($day)) { | |
$r['_specific_day'] = $day; | |
} | |
if (!is_null($year) || !is_null($month) || !is_null($day)) { | |
add_filter('getarchives_where', '__filter_wp_get_archives_specific', 10, 2); | |
} | |
$org_echo = (isset($args['echo']) ? $args['echo']:1); | |
$r['echo'] = 0; | |
$archive = wp_get_archives($r); | |
remove_filter('getarchives_where', '__filter_wp_get_archives_specific'); | |
if ($org_echo) { | |
echo $archive; | |
} | |
else { | |
return $archive; | |
} | |
} | |
/** | |
* Filter function used by wp_get_archives_specific() and the filter "getarchives_where" | |
* @param string $where | |
* @param string|array | |
* @return string | |
* @author David Mårtensson <[email protected]> | |
*/ | |
function __filter_wp_get_archives_specific($where, $args) { | |
global $wpdb; | |
if (isset($args['_specific_year']) && !empty($args['_specific_year'])) { | |
$where .= " AND YEAR(post_date) = '".$wpdb->escape($args['_specific_year'])."'"; | |
} | |
if (isset($args['_specific_month']) && !empty($args['_specific_month'])) { | |
$where .= " AND MONTH(post_date) = '".$wpdb->escape($args['_specific_month'])."'"; | |
} | |
if (isset($args['_specific_day']) && !empty($args['_specific_day'])) { | |
$where .= " AND DAY(post_date) = '".$wpdb->escape($args['_specific_day'])."'"; | |
} | |
return $where; | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment