Skip to content

Instantly share code, notes, and snippets.

@chiroleen
Created July 23, 2022 03:44
Show Gist options
  • Save chiroleen/c7c409389af000d83f0305c56aba2c42 to your computer and use it in GitHub Desktop.
Save chiroleen/c7c409389af000d83f0305c56aba2c42 to your computer and use it in GitHub Desktop.
<!-- Monthly Archive -->
<div class="widget">
<h4 class="monthly-archive-title"><i class="icon-calendar"></i> 月別アーカイブ</h4>
<?php
$monthly_archives = wp_get_archives(
$args = array(
'type' => 'monthly',
'show_post_count' => true,
'format' => 'custom', //added 220718
'before' => '<li>', //changed 220718
'after' => '</li>,', //changed 220718
'echo' => 0,
));
$monthly_archives = explode(',', $monthly_archives); //配列化
array_pop($monthly_archives); //末尾の空白要素を削除
//var_dump($monthly_archives);
$yearly_archives = wp_get_archives(
$args = array(
'type' => 'yearly',
'format' => 'custom',
'before' => '',
'after' => ',',
'echo' => 0,
));
$yearly_archives = explode(',', $yearly_archives); //配列化
array_pop($yearly_archives); //末尾の空白要素を削除
//var_dump($yearly_archives);
$this_year = (string)idate('Y'); //今年4桁
$out = '<ul class="archive-list">';
foreach ($yearly_archives as $year) {
$the_year = substr($year,-8,4);
if ($the_year === $this_year):
$out .= '<li class="year acv_open current">' . $the_year;
$out .= '<ul class="month-archive-list">';
else:
$out .= '<li class="year">' . $the_year;
$out .= '<ul class="month-archive-list">';
endif;
foreach ($monthly_archives as $month) {
$pos = strpos($month, $the_year); //月毎アーカイブの文字列中に、ターゲットとなる年が含まれているか
if ($pos !== false):
$out .= $month;
endif;
}
$out .= '</ul>';
$out .= '</li>'; // 閉じる <li class="year"> //position changed 220718
}
$out .= '</ul>'; // 閉じる <ul class="archive-list">
echo $out;
//var_dump($out);
?>
</div>
<!-- /Monthly Archive -->
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment