/**
 *
 * Display related posts component
 * @param     $category
 * @param     $post_ID
 * @param     $display_post_limit
 * @param int $columns_per_slide
 *
 * @return mixed
 */
function maxflat_get_related_post_box($category, $post_ID, $display_post_limit, $columns_per_slide = 4)
{
    $query = __MAXFLAT::layout()->get_related_post_box($category, $post_ID, $display_post_limit, $columns_per_slide);
    $limit = $query->found_posts;
    if ($limit != 0) {
        ?>

	<section class="smartlib-related-posts">
	    <h3><?php 
        _e('Related posts', 'maxflat');
        ?>
</h3>
			<div class="smartlib-slider-container">
				<ul class="smartlib-slides slider-list slides">
					<?php 
        $i = 1;
        $j = 1;
        while ($query->have_posts()) {
            $query->the_post();
            $post_format = get_post_format();
            if ($i == 1) {
                ?>

								<li class="row">
								<?php 
            }
            ?>

						<div class="columns large-4">
							<?php 
            if ('' != get_the_post_thumbnail()) {
                ?>


									<a href="<?php 
                the_permalink();
                ?>
" class="smartlib-thumbnail-outer"
										 title="<?php 
                echo esc_attr(sprintf(__('Permalink to %s', 'maxflat'), the_title_attribute('echo=0')));
                ?>
"
											><?php 
                maxflat_get_format_ico($post_format);
                the_post_thumbnail('medium-image-thumb');
                ?>
</a>

								<?php 
            } elseif ($post_format == 'gallery') {
                $featured_image = maxflat_get_featured_image('medium-image-thumb');
                if (!empty($featured_image)) {
                    ?>



										<a href="<?php 
                    the_permalink();
                    ?>
" class="smartlib-thumbnail-outer"
											 title="<?php 
                    echo esc_attr(sprintf(__('Permalink to %s', 'maxflat'), the_title_attribute('echo=0')));
                    ?>
"
												><?php 
                    maxflat_get_format_ico($post_format);
                    echo $featured_image;
                    ?>
</a>

									<?php 
                }
            }
            ?>

							<h4><a href="<?php 
            the_permalink();
            ?>
"><?php 
            the_title();
            ?>
</a></h4>
							<?php 
            if (empty($featured_image) && '' == get_the_post_thumbnail()) {
                ?>

						<?php 
                maxflat_display_meta_post('author');
                ?>

							<?php 
            }
            ?>

						</div>

						<?php 
            if ($i % $columns_per_slide == 0 || $j == $limit) {
                ?>

						</li>
								<?php 
                $i = 1;
            } else {
                $i++;
            }
            $j++;
        }
        // end while
        wp_reset_postdata();
        ?>

				</ul>
			</div>


	</section>
		<?php 
    }
}
    ?>
"
																																										 ><?php 
    the_post_thumbnail('medium-square');
    ?>
</a></div>

                    <?php 
} elseif ($post_format == 'gallery') {
    $featured_image = maxflat_featured_image('medium-square');
    if (!empty($featured_image)) {
        ?>


									<div class="smartlib-thumbnail-outer"><?php 
        maxflat_get_format_ico($post_format);
        ?>
<a href="<?php 
        the_permalink();
        ?>
"
																																												 title="<?php 
        echo esc_attr(sprintf(__('Permalink to %s', 'maxflat'), the_title_attribute('echo=0')));
        ?>
"
											><?php 
        echo $featured_image;
        ?>
</a></div>

									<?php 
    function widget($args, $instance)
    {
        $title = apply_filters('widget_title', $instance['title']);
        $limit = is_int($instance['gallery_limit']) ? $instance['gallery_limit'] : 4;
        extract($args);
        echo $before_widget;
        ?>

	<?php 
        if ($title) {
            echo $before_title . $title . $after_title;
        }
        $query = new WP_Query(array('posts_per_page' => $limit, 'tax_query' => array(array('taxonomy' => 'post_format', 'field' => 'slug', 'terms' => array('post-format-gallery')))));
        if ($query->have_posts()) {
            ?>



			<ul class="small-block-grid-2">
				<?php 
            while ($query->have_posts()) {
                $query->the_post();
                ?>


							<?php 
                $featured_image = __MAXFLAT::layout()->get_featured_image('medium-square');
                if ('' != get_the_post_thumbnail()) {
                    ?>

								<li>
					<a href="<?php 
                    the_permalink();
                    ?>
" class="smartlib-thumbnail-outer"><?php 
                    maxflat_get_format_ico('gallery');
                    the_post_thumbnail('medium-square');
                    ?>
</a>
								</li>
								<?php 
                } else {
                    if (!empty($featured_image)) {
                        ?>

									<li>
						<a href="<?php 
                        the_permalink();
                        ?>
" class="smartlib-thumbnail-outer"><?php 
                        maxflat_get_format_ico('gallery');
                        echo $featured_image;
                        ?>
</a></li>
								<?php 
                    }
                }
                ?>


					<?php 
            }
            wp_reset_postdata();
            ?>

			</ul>

		<?php 
            echo $after_widget;
            ?>

		<?php 
        }
    }