<?php

if (is_single()) {
    $get_by = get_theme_mod('post_get_by', 'category');
    if ('hide' != $get_by) {
        $limit = (int) get_theme_mod('post_relate_limit', 6);
        if ($limit > 0) {
            global $post;
            $taxs = array();
            if ('category' == $get_by) {
                $cats = get_the_category(($post->ID));
                if ($cats) {
                    $ids = array();
                    foreach ($cats as $cat) {
                        $ids[] = $cat->term_id;
                    }
                    $taxs [] = array(
                        'taxonomy' => 'category',
                        'field' => 'id',
                        'terms' => $ids
                    );
                }
            } else {
                $tags = get_the_tags($post->ID);
                if ($tags) {
                    $ids = array();
                    foreach ($tags as $tag) {
                        $ids[] = $tag->term_id;
                    }
                    $taxs [] = array(
                        'taxonomy' => 'post_tag',
                        'field' => 'id',
                        'terms' => $ids
                    );
                }
            }

            if ($taxs) {
                $related_args = array(
                    'tax_query' => $taxs,
                    'post__not_in' => array($post->ID),
                    'posts_per_page' => $limit
                );
                $related_posts = new WP_Query($related_args);
                if ($related_posts->have_posts()) {

                    ?>

                <div class="kopa-related-post article-list-2<?php if(!comments_open()) echo ' kopa-comment-disable'; ?>">
                    <h3> <?php echo esc_html__('Related Post','garajez-lite'); ?></h3>
                    <ul class="row clearfix">
                        <?php while($related_posts->have_posts()){
                        $related_posts->the_post();
                        $post_relate_custom_length = get_theme_mod('post_relate_custom_length', 55);
                        $post_excerpt = kopa_get_the_excerpt_for_widget(get_the_excerpt(), get_the_content(get_the_ID()), $post_relate_custom_length);
                        $post_col = 12/intval($limit);
                        ?>
                        <li class="col-md-<?php echo esc_attr($post_col); ?> col-sm-<?php echo esc_attr($post_col); ?> col-xs-<?php echo esc_attr($post_col); ?>">
                            <article class="entry-item">
                                <header>
                                    <span class="entry-date"><?php echo esc_html(get_the_date('F j,Y')); ?></span>
                                    <span class="rotate-line"></span>
                                                <span class="entry-comment">
                                                    <?php if(comments_open() && !post_password_required()){ ?>
                                                    <?php comments_popup_link(__('No comment', 'garajez-lite'), '1 '.__('comment', 'garajez-lite'), '% '. esc_attr__('comments', 'garajez-lite')); ?>
                                                    <?php } else {
                                                    echo esc_html__('Comment is disabled', 'garajez-lite');
                                                }?>
                                                </span>
                                </header>
                                <div class="entry-thumb">
                                    <a href="<?php the_permalink(); ?>" title="<?php the_title();?>">
                                        <?php
                                        if(has_post_thumbnail()){
                                            echo kopa_get_image(get_the_ID(),get_the_title(),100,100,true);
                                        }
                                        ?>
                                    </a>
                                </div>
                                <div class="entry-content">
                                    <h4 class="entry-title"><a href="<?php the_permalink(); ?>" title="<?php the_title(); ?>"><?php the_title(); ?></a></h4>
                                    <?php if(!empty($post_excerpt)){ ?>
                                    <p><?php echo esc_html($post_excerpt); ?></p>
                                    <?php } ?>
                                </div>
                                <a href="<?php the_permalink(); ?>" title="<?php the_title();?>" class="more-link style1"><?php echo esc_html__('Read more','garajez-lite'); ?></a>
                            </article>
                        </li>
                        <?php
                    } ?>
                    </ul>
                </div>

                <?php
                } // endif
                wp_reset_postdata();
            }
        }
    }
}