<div class="side_archive_menu">
<?php
$year_prev = null;
$months = $wpdb->get_results("SELECT DISTINCT MONTH( post_date ) AS month ,
YEAR( post_date ) AS year,
COUNT( id ) as post_count FROM $wpdb->posts
WHERE post_status = 'publish' and post_date <= now( )
and post_type = 'post'
GROUP BY month , year
ORDER BY post_date DESC");
foreach($months as $month) :
$year_current = $month->year;
if ($year_current != $year_prev){
if ($year_prev != null){?>
<?php } ?>
<p class="toggle_btn"><?php echo $month->year; ?>年</p>
<ul class="child">
<?php } ?>
<li>
<a href="<?php bloginfo('url') ?>/<?php echo $month->year; ?>/<?php echo date("m", mktime(0, 0, 0, $month->month, 1, $month->year)) ?>">
<?php echo date("n", mktime(0, 0, 0, $month->month, 1, $month->year)) ?>月
(<?php echo $month->post_count; ?>)
</a>
</li>
<?php $year_prev = $year_current;
endforeach; ?>
</ul>
</div>
<!-- /.side_archive_menu -->
<div class="side_archive_menu">
<p class="toggle_btn">2018年</p>
<ul class="child">
<li><a href="">11月(1)</a></li>
<li><a href="">10月(14)</a></li>
<li><a href="">9月(1)</a></li>
</ul>
</div>
<!-- /.side_archive_menu -->
.child {
display: none;
}
$(".toggle_btn").on("click", function() {
$(this).next().slideToggle();
$(this).toggleClass("active");
});