<div class="p-archive__search">
    <h2 class="p-post__theme">お知らせ検索</h2>
    <div class="p-archive__categories">
        <?php
        $terms = get_terms( 'news-cat');
        echo '<a href="'.esc_url(home_url('/news/')).'" class="p-archive__searchCategory">全体表示</a>';
        foreach ( $terms as $term ){
            echo '<a href="'.get_term_link($term->slug, 'news-cat').'" class="p-archive__searchCategory">'.$term->name.'</a>';
        }
        ?>
    </div>

    <ul class="p-archive__byYear">
        <?php
        $query_args = array(
            'post_type'      => 'news',
            'posts_per_page' => 1,
        );
        $last_post = get_posts( $query_args );
        $year = substr($last_post[0]->post_date, 0, 4 );
        $oldest_post = $year - 7;
        echo '<a href="'.esc_url(home_url('/')).$oldest_post.'/?post_type=news">〜'.$oldest_post.'年</a>';
        for ($num = 6; $num >= 0 ; $num--) {
            $old = $year - $num;
            echo '<a href="'.esc_url(home_url('/')).$old.'/?post_type=news">'.$old.'年</a>';
        }
        ?>
    </ul>
</div>