<?php
/*
Template Name: Archive
Template Post Type: page
*/
get_header();
?>

<main class="layoutSingleColumn u-paddingTop50">
    <?php
    $args = [
        'posts_per_page' => -1,
        'post_type' => ['post'],
        'ignore_sticky_posts' => 1,
        'tax_query' => array(
            array(  // only show standard post format
                'taxonomy' => 'post_format',
                'field' => 'slug',
                'terms' => array('post-format-aside', 'post-format-audio', 'post-format-chat', 'post-format-gallery', 'post-format-image', 'post-format-link', 'post-format-quote', 'post-format-status', 'post-format-video'),
                'operator' => 'NOT IN'
            )
        )
    ];
    $the_query = new WP_Query($args);
    $arr = [];
    while ($the_query->have_posts()) : $the_query->the_post();
        $year_tmp = get_the_time('Y');
        $mon_tmp = get_the_time('n');
        if (!array_key_exists($year_tmp, $arr)) {
            $arr[$year_tmp] = [];
        }

        if (!array_key_exists($mon_tmp, $arr[$year_tmp])) {
            $arr[$year_tmp][$mon_tmp] = [];
        }

        $arr[$year_tmp][$mon_tmp][] = [
            'title' => get_the_title(),
            'link' => get_permalink(),
            'commentnum' => get_comments_number(),
            // 'views' => jaguar_post_view($post->ID),
            'date' => get_the_time('m-d'),
        ];

    endwhile;
    wp_reset_postdata();
    $output = '';
    foreach ($arr as $year => $year_post) {
        $output .= '<div class="jArchive--area"><h2 class="jArchive--monthly">' . $year . '</h2>';
        foreach ($year_post as $month => $month_post) {
            $output .=  '<div class="jArchive--list" data-year="' . $year . ' - ' . $month  . '">';
            foreach ($month_post as $value) {
                $output .= '<div class="jArchive--item"><div class="jArchive--title"><a href="' . $value['link'] . '">' . $value['title'] . '</a></div><div class="jArchive--meta">' . $value['date'] . '</div></div>';
            }
            $output .= '</div>';
        }
        $output .= '</div>';
    }
    echo $output;
    ?>
</main>

<?php get_footer(); ?>