<?php
/**
 * Template name: Magazine
 *
 * @package Multi
 */

get_header();
get_template_part( 'template-parts/top-slider' );
get_template_part( 'template-parts/content-start' );

$multimagazine_col_class        = 'col3';
$multimagazine_sidebar_position = get_post_meta( $multimagazine_current_page_id, 'multi_sidebar_position', true );
if ( empty( $multimagazine_sidebar_position ) ) {
	$multimagazine_sidebar_position = $multimagazine_sidebar_pos_global;
}
if ( 'off' !== $multimagazine_sidebar_position ) {
	$multimagazine_sidebar = get_post_meta( get_the_id(), 'multi_custom_sidebar', true ) ? get_post_meta( get_the_id(), 'multi_custom_sidebar', true ) : 'default';
	if ( 'no' !== $multimagazine_sidebar ) {
		$multimagazine_col_class = 'col2';
	}
}

?>
<div class="main" role="main">
	<?php
	if ( have_posts() ) :
		while ( have_posts() ) {
			the_post();
			get_template_part( 'post-list-parts/content' );
			wp_reset_postdata();
		}
	endif;
	?>

	<div class="columns cat-archive">
		<?php
		$multimagazine_options = array(
			'hierarchical' => 0,
			'parent'       => 0,
		);
		$multimagazine_cats    = get_categories( $multimagazine_options );
		foreach ( $multimagazine_cats as $multimagazine_cat ) :
			?>
		<div class="col <?php echo esc_attr( $multimagazine_col_class ); ?>">
			<h3 class="category-title"><a href="<?php echo esc_url( get_category_link( $multimagazine_cat->term_id ) ); ?>"><?php echo esc_html( $multimagazine_cat->name ); ?></a></h3>
			<?php

			$multimagazine_current_post_number = 0;
			$multimagazine_post_options        = array(
				'post_type'      => 'post',
				'post_status'    => 'publish',
				'posts_per_page' => 4,
				'cat'            => $multimagazine_cat->term_id,
			);
			$multimagazine_posts               = new WP_Query( $multimagazine_post_options );
			if ( $multimagazine_posts->have_posts() ) :
				while ( $multimagazine_posts->have_posts() ) :
					$multimagazine_posts->the_post();
					if ( 0 === $multimagazine_current_post_number ) :
						?>
						<p class="img">
						<?php if ( has_post_thumbnail() ) : ?>
							<a href="<?php the_permalink(); ?>"><?php the_post_thumbnail( 'multimagazine-thumbnail-medium' ); ?></a>
						<?php endif; ?>
						</p>
						<ul>
							<li>
								<h4><a href="<?php the_permalink(); ?>"><?php multimagazine_the_title(); ?></a></h4>
								<p><?php echo wp_kses_post( multimagazine_custom_excerpt( 20, $post ) ); ?></p>
							</li>
					<?php else : ?>
						<li><a href="<?php the_permalink(); ?>"><?php multimagazine_the_title(); ?></a></li>
						<?php
					endif;
				$multimagazine_current_post_number++;
				endwhile;
				wp_reset_postdata();
				?>
				</ul>
			<?php endif; ?>
			<p class="more"><a href="<?php echo esc_url( get_category_link( $multimagazine_cat->term_id ) ); ?>"><?php esc_html_e( 'More in', 'multimagazine' ); ?> <?php echo esc_html( $multimagazine_cat->name ); ?></a></p>
			</div><?php endforeach; ?>
	</div>
</div>
<?php
if ( 'off' !== $multimagazine_sidebar_position && 'no' !== $multimagazine_sidebar ) {
	$multimagazine_sidebar_name = ( $multimagazine_sidebar && 'default' !== $multimagazine_sidebar ) ? 'custom' : '';
	get_sidebar( $multimagazine_sidebar_name );
}
?>

<?php get_footer(); ?>
