<?php
/**
 * Related posts based on categories and tags.
 * 
 */

$lawn_care_related_posts_taxonomy = get_theme_mod( 'lawn_care_related_posts_taxonomy', 'category' );

$lawn_care_post_args = array(
    'posts_per_page'    => absint( get_theme_mod( 'lawn_care_related_posts_count', '3' ) ),
    'orderby'           => 'rand',
    'post__not_in'      => array( get_the_ID() ),
);

$lawn_care_tax_terms = wp_get_post_terms( get_the_ID(), 'category' );
$lawn_care_terms_ids = array();
foreach( $lawn_care_tax_terms as $tax_term ) {
	$lawn_care_terms_ids[] = $tax_term->term_id;
}

$lawn_care_post_args['category__in'] = $lawn_care_terms_ids; 

if(get_theme_mod('lawn_care_related_post',true)==1){

$lawn_care_related_posts = new WP_Query( $lawn_care_post_args );

if ( $lawn_care_related_posts->have_posts() ) : ?>
    <div class="related-post">
        <h3><?php echo esc_html(get_theme_mod('lawn_care_related_post_title','Related Post'));?></h3>
        <div class="row">
            <?php while ( $lawn_care_related_posts->have_posts() ) : $lawn_care_related_posts->the_post(); ?>
                <div class="col-xl-4 col-lg-6 col-md-6 col-12">
                    <article id="post-<?php the_ID(); ?>" <?php post_class('inner-service'); ?>>
                        <div class="post-main-box">
                            <?php if( get_theme_mod( 'lawn_care_related_image_hide_show',true) == 1) { ?>
                                <div class="box-image">
                                    <?php 
                                        if(has_post_thumbnail()) { 
                                          the_post_thumbnail(); 
                                        }
                                    ?>
                                </div>
                            <?php } ?>
                            <h2 class="section-title"><a href="<?php echo esc_url(get_permalink()); ?>"><?php the_title();?><span class="screen-reader-text"><?php the_title(); ?></span></a></h2>
                            <?php echo esc_html (lawn_care_edit_link()); ?>
                            <div class="new-text">
                                <div class="entry-content">
                                    <?php $lawn_care_theme_lay = get_theme_mod( 'lawn_care_excerpt_settings','Excerpt');
                                        if($lawn_care_theme_lay == 'Content'){ ?>
                                          <?php the_content(); ?>
                                        <?php }
                                        if($lawn_care_theme_lay == 'Excerpt'){ ?>
                                          <?php if(get_the_excerpt()) { ?>
                                            <p><?php $lawn_care_excerpt = get_the_excerpt(); echo esc_html( lawn_care_string_limit_words( $lawn_care_excerpt, esc_attr(get_theme_mod('lawn_care_related_posts_excerpt_number','30')))); ?></p>
                                          <?php }?>
                                        <?php }?>
                                </div>
                            </div>
                            <?php if( get_theme_mod('lawn_care_related_button_text','Read More') != ''){ ?>
                                <div class="more-btn">
                                    <a href="<?php echo esc_url(get_permalink()); ?>"><?php echo esc_html(get_theme_mod('lawn_care_related_button_text',__('Read More','lawn-care')));?><span class="screen-reader-text"><?php echo esc_html(get_theme_mod('lawn_care_related_button_text',__('Read More','lawn-care')));?></span><span class="top-icon"></span></a>
                                </div>
                            <?php } ?>
                        </div>
                        <div class="clearfix"></div>
                    </article>
                </div>
            <?php endwhile; ?>
        </div>
    </div>
<?php endif;
wp_reset_postdata();

}