<?php if (!is_robots()) {
    set_post_views(get_the_ID());
}?>
<div class="p-side">
    <div class="p-side__search<?php if(is_search() || is_singular('blog') || is_tax('blog-cat') || is_tag()){echo ' p-side__search--single';} ?>">
        <?php get_search_form(); ?>
    </div>
    <div class="p-side__block">
        <h3 class="p-side__title">カテゴリー</h3>
        <div class="p-side__categories">
            <?php
            $terms = get_terms('blog-cat');
            if ( $terms ) {
                foreach ( $terms as $term ) {
                    echo '<a href="'.esc_url(get_term_link($term)).'" class="p-side__category">'.$term->name.'</a>';
                }
            }
            ?>
        </div>
    </div>
    <div class="p-side__block">
        <h3 class="p-side__title">月間ランキング</h3>
            <?php
            $args = array(
            'meta_key' => 'post_views_count',
            'post_type' => 'blog',
            'orderby' => 'meta_value_num',
            'order' => 'DESC',
            'posts_per_page' => 5
            );
            $query = new WP_Query($args);
            if ($query->have_posts()) :
            ?>
                <div class="p-side__ranking">
                    <?php
                    while ($query->have_posts()) :
                    $query->the_post();
                    ?>
                        <a href="<?php the_permalink(); ?>" class="p-side__item">
                            <time class="p-side__time" datetime="<?php the_time('c'); ?>"><?php the_time( 'Y.m.d' ); ?></time>
                            <p class="p-side__itemTitle"><?php the_title(); ?></p>
                        </a>
                    <?php endwhile; ?>
                </div>
            <?php
            endif;
            wp_reset_postdata();
            ?>
    </div>
    <div class="p-side__block">
        <h3 class="p-side__title">タグ一覧</h3>
            <?php
            $tags = get_tags();
            if($tags) :
            ?>
                <div class="p-side__categories">
                    <?php
                    if($tags){
                        foreach( $tags as $tag ) {
                            echo '<a href="'.get_tag_link($tag->term_id).'" class="p-side__category">'.$tag->name.'</a>';
                        }
                    }
                    ?>
                </div>
            <?php endif; ?>
    </div>
</div>