<?php
/**
 * Template part for displaying posts
 *
 * @link https://codex.wordpress.org/Template_Hierarchy
 * @since Blogto 1.0.0
 */

if( !blogto_get_option( 'disable_blog' ) && is_home() ):

	$cat_id = absint( blogto_get_option( 'blog_category' ) );

	$args = array(
		'posts_per_page' => absint( blogto_get_option( 'blog_number' ) ),
		'ignore_sticky_posts' => 1
	);

	if( $cat_id > 0 ){
		$args[ 'cat' ] = $cat_id;
	}
	$query = new WP_Query( apply_filters( 'blogto_blog_args', $args ) );

	if( $query->have_posts() ): 
?>
		<!-- Blog Category Section -->
		<section class="wrapper block-grid blog-section">
			<div class="container">
				<div class="section-title-group">
					<h2 class="section-title"><?php echo wp_kses_post( blogto_get_option( 'blog_section_title' ) ); ?></h2>
				</div>
			</div>
			<div class="container">
				<div class="row">
					<?php
						while( $query->have_posts() ):
							$query->the_post();
							?>
							<div class="col-xs-12 col-sm-4 col-md-4">
								<article id="post-<?php the_ID(); ?>" <?php post_class( 'post-content' ); ?>>

									<div class="post-thumb-outer text-center">
										<?php
										$args = array(
											'size' => 'blogto-1170-710',
										);

										blogto_post_thumbnail( $args );
										?>
									</div>

									<div class="post-content-inner">
									<?php
									if( 'post' == get_post_type() ):
										$cat = blogto_get_the_category();
										if( $cat ):
									?>
											<span class="cat">
												<?php
													$term_link = get_category_link( $cat[ 0 ]->term_id );
												?>
												<a href="<?php echo esc_url( $term_link ); ?>">
													<?php echo esc_html( $cat[0]->name ); ?>
												</a>
											</span>
									<?php
										endif;
									endif;
									?>
										<header class="post-title">
											<h3>
												<a href="<?php the_permalink(); ?>">
													<?php echo get_the_title(); ?>
												</a>
											</h3>
										</header>
										<div class="post-text"><?php blogto_excerpt( 15, true, '&hellip;' ); ?></div>
										<div class="button-container">
											<?php
												if( 'post' == get_post_type() ){
												?>	
												<div class="post-footer-detail">
													<?php if( 'post' == get_post_type() ): ?>
														<div class="post-format-outer">
															<span class="post-format">
																<span class="kfi <?php echo esc_attr( blogto_get_icon_by_post_format() ); ?>"></span>
															</span>
														</div>
													<?php endif; ?>
													<span class="author-name">
														<a href="<?php echo esc_url( get_author_posts_url( get_the_author_meta( 'ID' ) ) ); ?>">
															<span><?php echo esc_html( 'by:' ); ?></span>
															<?php echo get_the_author(); ?>
														</a>
													</span>
													&nbsp; &nbsp;<?php echo esc_html( '|', 'blogto' ); ?>&nbsp; &nbsp;
													<a href="<?php echo esc_url( blogto_get_day_link() ); ?>" class="date">
														<span class="day">
														<?php
															echo esc_html(get_the_date('M j, Y')); ?>
														</span>
													</a>
													&nbsp; &nbsp;<?php echo esc_html( '|', 'blogto' ); ?>&nbsp; &nbsp;
													<span class="comment-link">
														<a href="<?php comments_link(); ?>">
															<?php echo absint( wp_count_comments( get_the_ID() )->approved ); ?>
														</a>
													</span>
												</div>
											<?php } ?>
											<?php if( get_edit_post_link()){
												blogto_edit_link();
											} ?>
										</div>
									</div>
								</article>
							</div>
						<?php
						endwhile; 
						wp_reset_postdata(); 
					?>
				</div>
			</div>
		</section> <!-- End Blog Section -->
<?php 
	endif; 
endif;


