Created
September 21, 2015 06:34
-
-
Save s-hiroshi/ba92ae6cebd00d7b930a to your computer and use it in GitHub Desktop.
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 | |
/** | |
* Get archives markup. | |
* Customize wp-includes/general-template.php -> wp_get_archives | |
* @param string|array $args Optional. Override defaults. | |
* @return string|null String when retrieving, null when displaying. | |
*/ | |
function shintotsushin_wp_get_archives( $args = '' ) { | |
global $wp_query; | |
global $wpdb; | |
// Be used in category or single. | |
if ( ! is_category() and ! is_single() ) { | |
return; | |
} | |
// Get category id. | |
$cat_id = (int) $wp_query->query_vars['cat']; | |
// Set default parameter. | |
$defaults = array( | |
'type' => 'monthly', | |
'limit' => '', | |
'format' => 'html', | |
'before' => '', | |
'after' => '', | |
'show_post_count' => false, | |
'echo' => 1, | |
'order' => 'DESC', | |
); | |
// Crete parameter. | |
$args = wp_parse_args( $args, $defaults ); | |
// Create where condition of sql. | |
$type = ( '' === $args['type'] ) ? 'monthly' : $args['type']; | |
$limit = ''; | |
if ( '' !== $args['limit'] ) { | |
$limit = absint( $args['limit'] ); | |
$limit = ' LIMIT ' . $limit; | |
} | |
$order = strtoupper( $args['order'] ); | |
if ( 'ASC' !== $order ) { | |
$order = 'DESC'; | |
} | |
$where = apply_filters( | |
'getarchives_where', | |
"WHERE post_type = 'post' AND post_status = 'publish' AND t.term_taxonomy_id = " . $cat_id, | |
$args | |
); | |
$custom_sql = "LEFT JOIN $wpdb->term_relationships AS r ON $wpdb->posts.ID = r.object_ID LEFT JOIN $wpdb->term_taxonomy AS t ON r.term_taxonomy_id = t.term_taxonomy_id LEFT JOIN $wpdb->terms as terms ON t.term_id = terms.term_id"; | |
$join = apply_filters( 'getarchives_join', $custom_sql, $args ); | |
// Set return markup | |
$output = ''; | |
// Set cache | |
$last_changed = wp_cache_get( 'last_changed', 'posts' ); | |
if ( ! $last_changed ) { | |
$last_changed = microtime(); | |
wp_cache_set( 'last_changed', $last_changed, 'posts' ); | |
} | |
// Create monthly category archives query. | |
if ( 'monthly' === $type ) { | |
$query = "SELECT YEAR(post_date) AS `year`, MONTH(post_date) AS `month`, count(ID) AS posts FROM $wpdb->posts $join $where GROUP BY YEAR(post_date), MONTH(post_date) ORDER BY post_date $order $limit"; | |
$key = md5( $query ); | |
$key = "wp_get_archives:$key:$last_changed"; | |
if ( ! $results = wp_cache_get( $key, 'posts' ) ) { | |
$results = $wpdb->get_results( $query ); | |
wp_cache_set( $key, $results, 'posts' ); | |
} | |
if ( ! empty( $results ) ) { | |
$afterafter = $args['after']; | |
foreach ( (array) $results as $result ) { | |
$url = shintotsushin_get_link( $result->year, $result->month, $cat_id ); | |
$text = sprintf( __( '%2$d/%1$02d', 'cmarchives' ), $result->month, $result->year ); | |
$after = ''; | |
if ( $args['show_post_count'] ) { | |
$after = ' ( ' . $result->posts . ')' . $afterafter; | |
} | |
$output .= get_archives_link( $url, $text, $args['format'], $args['before'], $after ); | |
} | |
} | |
} | |
// Return monthly category arvhives. | |
if ( ! empty( $args['echo'] ) ) { | |
echo $output; | |
} else { | |
return $output; | |
} | |
} | |
/** | |
* Get link. | |
* Refer wp-includes/link-template.php->get_month_link | |
* @param bool|int $year False for current year. Integer of year. | |
* @param bool|int $month False for current month. Integer of month. | |
* @param int $cat_id Category ID. | |
* @return string | |
* @link http://wpdocs.sourceforge.jp/%E9%96%A2%E6%95%B0%E3%83%AA%E3%83%95%E3%82%A1%E3%83%AC%E3%83%B3%E3%82%B9/WP_Rewrite | |
* @version 1.0.0 | |
* @since 1.0.0 | |
*/ | |
function shintotsushin_get_link( $year, $month, $cat_id ) { | |
global $wp_rewrite; | |
if ( empty( $year ) ) { | |
$year = gmdate( 'Y', current_time( 'timestamp' ) ); | |
} | |
if ( empty( $month ) ) { | |
$month = gmdate( 'm', current_time( 'timestamp' ) ); | |
} | |
// Get monthly link. | |
$month_link = $wp_rewrite->get_month_permastruct(); | |
if ( ! empty( $month_link ) ) { | |
$month_link = str_replace( '%year%', $year, $month_link ); | |
$month_link = str_replace( '%monthnum%', zeroise( intval( $month ), 2 ), $month_link ); | |
// Add category id to query parameter. | |
return apply_filters( | |
'month_link', | |
home_url( user_trailingslashit( $month_link, 'month' ) ) . '?cat=' . (int) $cat_id, | |
$year, | |
$month | |
); | |
} else { | |
return apply_filters( | |
'month_link', | |
home_url( '?m=' . $year . zeroise( $month, 2 ) . '&cat=' . (int) $cat_id ), | |
$year, | |
$month | |
); | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment