<?php get_header(); ?>
<main class="corporate">
    <?php include('template/bottom-corporate-news.php'); ?>
    <?php include('template/breadcrumb.php'); ?>
    <section class="p-archive">
        <div class="p-archive__heading">
            <div class="p-archive__inner l-inner">
                <?php include('template/search.php'); ?>
                <div class="p-archive__contents">
                    <?php
                    $queried = get_queried_object();
                    $paged = get_query_var('paged') ? get_query_var('paged') : 1;
                    $the_query = new WP_Query();
                    $param = array(
                        'paged'          => $paged,
                        'post_type'      => 'news',
                        'posts_per_page' => '10',
                        'post_status'    => 'publish',
                        'tax_query' => array(
                            array(
                                'taxonomy' => 'news-cat',
                                'field' => 'slug',
                                'terms' => $queried->slug,
                                'operator' => 'IN'
                            ),
                        )
                    );
                    $the_query->query($param);
                    if ($the_query->have_posts()) :
                        echo '<div class="p-archive__posts">';
                        if(function_exists('wp_pagenavi')) {
                            echo '<div class="p-archive__pagenavi">';
                                wp_pagenavi(array('query'=>$the_query));
                            echo '</div>';
                        }
                    ?>
                        <div class="p-archive__items">
                            <?php while( $the_query->have_posts() ) : $the_query->the_post(); ?>
                                <div class="p-archive__item p-newsItem p-newsItem--archive">
                                    <a href="<?php the_permalink(); ?>" class="p-newsItem__img c-img">
                                        <?php
                                        if (has_post_thumbnail()) :
                                            the_post_thumbnail('post-thumbnail', array('alt' => the_title_attribute('echo=0')));
                                        else :
                                            echo '<img src="'.get_template_directory_uri().'/images/common/noimage.jpg" alt="'.esc_html(get_the_title()).'">';
                                        endif; ?>
                                    </a>
                                    <div class="p-newsItem__body">
                                        <div class="p-newsItem__info">
                                            <time class="p-newsItem__time" datetime="<?php the_time( 'c' ); ?>"><?php the_time( 'Y.m.d' ); ?></time>
                                            <?php
                                            $terms = get_the_terms($post->ID, 'news-cat');
                                            if ( $terms ) {
                                                echo '<div class="p-newsItem__categories">';
                                                    $num = 0;
                                                    foreach($terms as $term) {
                                                        if($num <= 1) :
                                                            echo '<a href="'.esc_url(get_term_link($term)).'" class="p-newsItem__category">'.$term->name.'</a>';
                                                        endif;
                                                        $num++;
                                                    }
                                                echo '</div>';
                                            }
                                            ?>
                                        </div>
                                        <p class="p-newsItem__title"><?php the_title(); ?></p>
                                        <p class="p-newsItem__text"><?php remove_filter('the_excerpt', 'wpautop'); the_excerpt();?></p>
                                        <a href="<?php the_permalink(); ?>" class="p-newsItem__link">続きを読む</a>
                                    </div>
                                </div>
                            <?php endwhile; ?>
                        </div>
                    <?php
                    endif;
                    wp_reset_postdata();
                    ?>
                </div>
            </div>
        </div>
    </section>
</main>
<?php get_footer();