てくてくあるく

WordPress の テーマ とか プラグイン に ついて 勉強しています

wp_get_archives を 使って 表記は 英語にしたい ということがあったので 覚書!!

でも 冗長的なので 他に もっと良い方法があると思います…

Deblogを参考にさせていただきました。
http://dejune.net/deblog/unclassified/post/20131205191716.html
<?php
//---------------------------------------------------------------------------
//  カスタムロケール
//---------------------------------------------------------------------------

if ( ! class_exists( 'custom_Locale' ) ) {
  class custom_Locale extends WP_Locale {
    function init() {
      $this->weekday[0] = 'Sunday';
      $this->weekday[1] = 'Monday';
      $this->weekday[2] = 'Tuesday';
      $this->weekday[3] = 'Wednesday';
      $this->weekday[4] = 'Thursday';
      $this->weekday[5] = 'Friday';
      $this->weekday[6] = 'Saturday';

      $this->weekday_initial['Sunday']    = 'S_Sunday_initial';
      $this->weekday_initial['Monday']    = 'M_Monday_initial';
      $this->weekday_initial['Tuesday']   = 'T_Tuesday_initial';
      $this->weekday_initial['Wednesday'] = 'W_Wednesday_initial';
      $this->weekday_initial['Thursday']  = 'T_Thursday_initial';
      $this->weekday_initial['Friday']    = 'F_Friday_initial';
      $this->weekday_initial['Saturday']  = 'S_Saturday_initial';

      foreach ($this->weekday_initial as $weekday_ => $weekday_initial_) {
        $this->weekday_initial[$weekday_] = preg_replace('/_.+_initial$/', '', $weekday_initial_);
      }

      $this->weekday_abbrev['Sunday']    = 'Sun';
      $this->weekday_abbrev['Monday']    = 'Mon';
      $this->weekday_abbrev['Tuesday']   = 'Tue';
      $this->weekday_abbrev['Wednesday'] = 'Wed';
      $this->weekday_abbrev['Thursday']  = 'Thu';
      $this->weekday_abbrev['Friday']    = 'Fri';
      $this->weekday_abbrev['Saturday']  = 'Sat';

      $this->month['01'] = 'January';
      $this->month['02'] = 'February';
      $this->month['03'] = 'March';
      $this->month['04'] = 'April';
      $this->month['05'] = 'May';
      $this->month['06'] = 'June';
      $this->month['07'] = 'July';
      $this->month['08'] = 'August';
      $this->month['09'] = 'September';
      $this->month['10'] = 'October';
      $this->month['11'] = 'November';
      $this->month['12'] = 'December';

      $this->month_abbrev['January']   = 'Jan_January_abbreviation';
      $this->month_abbrev['February']  = 'Feb_February_abbreviation';
      $this->month_abbrev['March']     = 'Mar_March_abbreviation';
      $this->month_abbrev['April']     = 'Apr_April_abbreviation';
      $this->month_abbrev['May']       = 'May_May_abbreviation';
      $this->month_abbrev['June']      = 'Jun_June_abbreviation';
      $this->month_abbrev['July']      = 'Jul_July_abbreviation';
      $this->month_abbrev['August']    = 'Aug_August_abbreviation';
      $this->month_abbrev['September'] = 'Sep_September_abbreviation';
      $this->month_abbrev['October']   = 'Oct_October_abbreviation';
      $this->month_abbrev['November']  = 'Nov_November_abbreviation';
      $this->month_abbrev['December']  = 'Dec_December_abbreviation';

      foreach ($this->month_abbrev as $month_ => $month_abbrev_) {
        $this->month_abbrev[$month_] = preg_replace('/_.+_abbreviation$/', '', $month_abbrev_);
      }

      $this->meridiem['am'] = 'am';
      $this->meridiem['pm'] = 'pm';
      $this->meridiem['AM'] = 'AM';
      $this->meridiem['PM'] = 'PM';

      $trans = __('number_format_thousands_sep');
      $this->number_format['thousands_sep'] = ('number_format_thousands_sep' == $trans) ? ',' : $trans;

      $trans = __('number_format_decimal_point');
      $this->number_format['decimal_point'] = ('number_format_decimal_point' == $trans) ? '.' : $trans;

      if ( isset( $GLOBALS['text_direction'] ) )
        $this->text_direction = $GLOBALS['text_direction'];
      elseif ( 'rtl' == _x( 'ltr', 'text direction' ) )
        $this->text_direction = 'rtl';
    }
  }
}

//---------------------------------------------------------------------------
//  カスタムアーカイブ
//---------------------------------------------------------------------------

if( !function_exists( 'custom_get_archives' ) ) {
  function custom_get_archives( $args = '' ) {
    global $wpdb;
    $wp_locale = new custom_Locale;
    $defaults = array(
      'type' => 'monthly', 'limit' => '',
      'format' => 'html', 'before' => '',
      'after' => '', 'show_post_count' => false,
      'echo' => 1, 'order' => 'DESC',
    );
    $r = wp_parse_args( $args, $defaults );
    if ( '' == $r['type'] ) {
      $r['type'] = 'monthly';
    }
    if ( ! empty( $r['limit'] ) ) {
      $r['limit'] = absint( $r['limit'] );
      $r['limit'] = ' LIMIT ' . $r['limit'];
    }
    $order = strtoupper( $r['order'] );
    if ( $order !== 'ASC' ) {
      $order = 'DESC';
    }
    // this is what will separate dates on weekly archive links
    $archive_week_separator = '&#8211;';
    // over-ride general date format ? 0 = no: use the date format set in Options, 1 = yes: over-ride
    $archive_date_format_over_ride = 0;
    // options for daily archive (only if you over-ride the general date format)
    $archive_day_date_format = 'Y/m/d';
    // options for weekly archive (only if you over-ride the general date format)
    $archive_week_start_date_format = 'Y/m/d';
    $archive_week_end_date_format   = 'Y/m/d';
    if ( ! $archive_date_format_over_ride ) {
      $archive_day_date_format = get_option( 'date_format' );
      $archive_week_start_date_format = get_option( 'date_format' );
      $archive_week_end_date_format = get_option( 'date_format' );
    }
    /**
     * Filter the SQL WHERE clause for retrieving archives.
     *
     * @since 2.2.0
     *
     * @param string $sql_where Portion of SQL query containing the WHERE clause.
     * @param array  $r   An array of default arguments.
     */
    $where = apply_filters( 'getarchives_where', "WHERE post_type = 'post' AND post_status = 'publish'", $r );
    /**
     * Filter the SQL JOIN clause for retrieving archives.
     *
     * @since 2.2.0
     *
     * @param string $sql_join Portion of SQL query containing JOIN clause.
     * @param array  $r  An array of default arguments.
     */
    $join = apply_filters( 'getarchives_join', '', $r );
    $output = '';
    $last_changed = wp_cache_get( 'last_changed', 'posts' );
    if ( ! $last_changed ) {
      $last_changed = microtime();
      wp_cache_set( 'last_changed', $last_changed, 'posts' );
    }
    $limit = $r['limit'];
    if ( 'monthly' == $r['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 ( $results ) {
        $after = $r['after'];
        foreach ( (array) $results as $result ) {
          $url = get_month_link( $result->year, $result->month );
          /* translators: 1: month name, 2: 4-digit year */
          $text = sprintf( '%1$s.%2$d', $wp_locale->get_month( $result->month ), $result->year );
          if ( $r['show_post_count'] ) {
            $r['after'] = '&nbsp;(' . $result->posts . ')' . $after;
          }
          $output .= get_archives_link( $url, $text, $r['format'], $r['before'], $r['after'] );
        }
      }
    } elseif ( 'yearly' == $r['type'] ) {
      $query = "SELECT YEAR(post_date) AS `year`, count(ID) as posts FROM $wpdb->posts $join $where GROUP BY YEAR(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 ( $results ) {
        $after = $r['after'];
        foreach ( (array) $results as $result) {
          $url = get_year_link( $result->year );
          $text = sprintf( '%d', $result->year );
          if ( $r['show_post_count'] ) {
            $r['after'] = '&nbsp;(' . $result->posts . ')' . $after;
          }
          $output .= get_archives_link( $url, $text, $r['format'], $r['before'], $r['after'] );
        }
      }
    } elseif ( 'daily' == $r['type'] ) {
      $query = "SELECT YEAR(post_date) AS `year`, MONTH(post_date) AS `month`, DAYOFMONTH(post_date) AS `dayofmonth`, count(ID) as posts FROM $wpdb->posts $join $where GROUP BY YEAR(post_date), MONTH(post_date), DAYOFMONTH(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 );
        $cache[ $key ] = $results;
        wp_cache_set( $key, $results, 'posts' );
      }
      if ( $results ) {
        $after = $r['after'];
        foreach ( (array) $results as $result ) {
          $url  = get_day_link( $result->year, $result->month, $result->dayofmonth );
          $date = sprintf( '%1$d-%2$02d-%3$02d 00:00:00', $result->year, $result->month, $result->dayofmonth );
          $text = mysql2date( $archive_day_date_format, $date );
          if ( $r['show_post_count'] ) {
            $r['after'] = '&nbsp;(' . $result->posts . ')' . $after;
          }
          $output .= get_archives_link( $url, $text, $r['format'], $r['before'], $r['after'] );
        }
      }
    } elseif ( 'weekly' == $r['type'] ) {
      $week = _wp_mysql_week( '`post_date`' );
      $query = "SELECT DISTINCT $week AS `week`, YEAR( `post_date` ) AS `yr`, DATE_FORMAT( `post_date`, '%Y-%m-%d' ) AS `yyyymmdd`, count( `ID` ) AS `posts` FROM `$wpdb->posts` $join $where GROUP BY $week, YEAR( `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' );
      }
      $arc_w_last = '';
      if ( $results ) {
        $after = $r['after'];
        foreach ( (array) $results as $result ) {
          if ( $result->week != $arc_w_last ) {
            $arc_year       = $result->yr;
            $arc_w_last     = $result->week;
            $arc_week       = get_weekstartend( $result->yyyymmdd, get_option( 'start_of_week' ) );
            $arc_week_start = date_i18n( $archive_week_start_date_format, $arc_week['start'] );
            $arc_week_end   = date_i18n( $archive_week_end_date_format, $arc_week['end'] );
            $url      = sprintf( '%1$s/%2$s%3$sm%4$s%5$s%6$sw%7$s%8$d', home_url(), '', '?', '=', $arc_year, '&amp;', '=', $result->week );
            $text     = $arc_week_start . $archive_week_separator . $arc_week_end;
            if ( $r['show_post_count'] ) {
              $r['after'] = '&nbsp;(' . $result->posts . ')' . $after;
            }
            $output .= get_archives_link( $url, $text, $r['format'], $r['before'], $r['after'] );
          }
        }
      }
    } elseif ( ( 'postbypost' == $r['type'] ) || ('alpha' == $r['type'] ) ) {
      $orderby = ( 'alpha' == $r['type'] ) ? 'post_title ASC ' : 'post_date DESC ';
      $query = "SELECT * FROM $wpdb->posts $join $where ORDER BY $orderby $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 ( $results ) {
        foreach ( (array) $results as $result ) {
          if ( $result->post_date != '0000-00-00 00:00:00' ) {
            $url = get_permalink( $result );
            if ( $result->post_title ) {
              /** This filter is documented in wp-includes/post-template.php */
              $text = strip_tags( apply_filters( 'the_title', $result->post_title, $result->ID ) );
            } else {
              $text = $result->ID;
            }
            $output .= get_archives_link( $url, $text, $r['format'], $r['before'], $r['after'] );
          }
        }
      }
    }
    if ( $r['echo'] ) {
      echo $output;
    } else {
      return $output;
    }
  }
}